VBA使代码更简单

时间:2016-12-20 12:37:53

标签: excel-vba vba excel

请查看我的代码。有两个数组做同样但二维数组我不能用于Application.match()。(我正在销售我每天都有销售的报告。接下来我用我的数组来计算利润)

'****************************
Dim products As Variant
Dim productsWithParams As Variant
Dim pos, buyPrice, profit
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

products = Array("MS-CHOPMAT-6", "MS-BOARDS-3", "DEM-CROC-34")
productsWithParams = Array(Array("MS-CHOPMAT-6", 10), Array("MS-BOARDS-3", 10), Array("DEM-CROC-34", 10))
For x = LastRow To 2 Step -1
  order_quantity = Range("$E$" & x).Value
  item_price = Range("$F$" & x).Value

  ' if value not found inside the array using the "MATCH" function
  pos = Application.Match(Range("$D$" & x).Value, products, 0)
  If IsError(pos) Then
    Range("$H$" & x).Value = "ERROR - " 
  Else ' successful "MATCH" inside the array
    Range("$H$" & x).Value = order_quantity * 3
    buyPrice = productsWithParams(pos - 1)(1)
    profit = (item_price - buyPrice) * order_quantity
    Range("$L$" & x).Value = pos
    Range("$M$" & x).Value = buyPrice
    Range("$N$" & x).Value = profit
  End If
Next

0 个答案:

没有答案