EXCEL VBA 用于排列组合计算

EXCEL VBA for permutation and combinations calculations

我是 Excel VBA 的新手,我正在尝试编写代码来完成一项简单的任务。

一个Excel中有3个作品sheet:SummaryDiscountPrice

作品sheet Summary包含如下数据:

下拉列表B1B3中的数据):

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

作品sheet Discount包含如下数据:

作品sheet Price包含如下数据:

任务:

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

对任何错误表示歉意,有人可以解决这个问题吗? 如有任何问题,请告诉我

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

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

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