EXCEL VBA用于排列和组合计算

问题描述

我对Excel VBA完全陌生,我正在尝试编写代码来完成简单的任务。

Excel中有3个工作表:SummarydiscountPrice

工作表Summary包含以下数据:

img

下拉B1B3中的数据):

Type: Supermarket,Hopcoms
Product:Dairy,Meat,Combo
Category:Chicken,Eggs,Eggs+Chicken

工作表discount包含以下数据:

工作表Price包含以下数据:

img

任务:

  1. 摘要表中的用户通过下拉列表在B1:B3中选择数据。
  2. 基于选择,应从折扣表中为相应的组合选择%的折扣 例如:如果用户摘要表中选择Supermarket-Combo-Eggs + Chicken(来自#1),则总折扣应为13%(根据折扣表)。
  3. 用户导航至价格表,然后在“总价格”列中为每个客户输入数据
  4. 用户单击计算折扣按钮时,现在应为所有客户的总价应用13%的折扣,并在“可用折扣”列中更新折扣金额
  5. 提供给所有客户的总折扣价应在T.discount旁边更新。

对任何错误表示歉意,谁能解决这个问题? 如有任何问题,请告诉我

解决方法

请测试下一个代码。但在此之前,请在Price工作表中的现有“客户”(B:B)和“总价”(C:C)之间插入另一列,并将其命名为“ Price”。用户将在那里填写标准价格(无折扣),并在“总价”中使用折扣计算价格:

Sub TestApplyDiscount()
 Dim shS As Worksheet,shD As Worksheet,shP As Worksheet,lastRowP As Long
 Dim strType As String,strProd As String,strCat As String,i As Long,j As Long
 Dim rowType As Long,rowProd As Long,rowCat As Long,disc As Double
 
 Set shS = Worksheets("Summary")
 Set shD = Worksheets("Discount")
 Set shP = Worksheets("Price")
 lastRowP = shP.Range("A" & Rows.count).End(xlUp).Row
 
 strType = shS.Range("B1").value
 strProd = shS.Range("B2").value
 strCat = shS.Range("B3").value
 
 With WorksheetFunction
       rowType = .Match(strType,shD.Range("A1:A19"),0)

        rowProd = .Match(strProd,shD.Range("B" & rowType & ":B" & rowType + 8),0)
        rowProd = rowProd + rowType - 1
        
         rowCat = .Match(strCat,shD.Range("C" & rowProd & ":C" & rowProd + 3),0)
         rowCat = rowCat + rowProd - 1
         
         disc = shD.Range("D" & rowCat).value
 End With
 For i = 2 To lastRowP
    shP.Range("E" & i).value = disc & "%"
    shP.Range("D" & i).FormulaR1C1 = "=RC[-1]-(RC[-1]*RC[1])"
 Next i
End Sub

我正在等待一些反馈...