Excel VBA在列中查找值,并做math

我有

A栏 – 名称
B栏 – 含数量
C栏 – 我想要返回的数量x成本的价值
列E – 与名字,但位于不同的单元格
F栏 – 有价格

我试图实现的是:从A1中获取价值,并在E:E中find它(例如,我们在E10中find它),当findB1的值并将其乘以F10的相应值 – 并将所有这些C栏

A列中的所有值

我试图用Do和两个variablesx和y来完成,但是由于某些原因,它并没有为一些行find所有的值。

先谢谢你。

Sub update_button() 'calculates money value for amazon sku Dim x, y, z As Integer 'x, y, and z variables function as loop counters 'Loop through added SKU/Prices For x = 4 To 25000 If Worksheets("Sheet1").Range("H" & x) = "" Then 'Blank row found, exit the loop Exit For End If 'Loop through Column E to find the first blank row to add the value from H into For y = 4 To 25000 If Worksheets("Sheet1").Range("E" & y) = "" Then 'Blank row found, Add SKU and Price Worksheets("Sheet1").Range("E" & y) = Worksheets("Sheet1").Range("H" & x) Worksheets("Sheet1").Range("F" & y) = Worksheets("Sheet1").Range("I" & x) 'Blank out Columns H and I to prevent need to do it manually Worksheets("Sheet1").Range("H" & x) = "" Worksheets("Sheet1").Range("I" & x) = "" Exit For End If Next y Next x '---NOW THIS IS WHERE I HAVE THE PROBLEM 'Get Values Dim intCumulativePrice As Integer 'Loop through report tab and get SKU x = 4 'initialize x to the first row of data on the Sheet1 tab Do While Worksheets("Sheet1").Range("A" & x) <> "" 'Loop through valid SKU's to find price of item y = 4 'initialize y to the first row of SKUs on the Sheet1 tab Do While Worksheets("Sheet1").Range("E" & y) <> "" If Worksheets("Sheet1").Range("E" & x) = Worksheets("Sheet1").Range("A" & y) Then 'Check if current SKU on Sheet1 tab matches the current SKU from SKU list 'Calculates the total intCumulativePrice = intCumulativePrice + (Worksheets("Sheet1").Range("B" & y) * Worksheets("Sheet1").Range("F" & x)) ' Puts Quantity X Price in Column B agains every Cell Worksheets("Sheet1").Range("C" & y) = (Worksheets("Sheet1").Range("B" & y) * Worksheets("Sheet1").Range("F" & x)) Exit Do End If y = y + 1 Loop x = x + 1 Loop 'Puts Grand total in Column L Cell 4 Worksheets("Sheet1").Range("L4") = intCumulativePrice 'Show messagebox to show that report processing has completed MsgBox "Report processing has been completed successfully", vbInformation, "Processing Complete!" End Sub 

要用VBA做到这一点,你应该将源数据复制到Variant数组并循环。 更快和IMO更容易阅读和debugging。

像这样的东西

 Sub Demo() Dim Dat As Variant Dim PriceList As Range Dim PriceListNames As Variant Dim PriceListPrices As Variant Dim Res As Variant Dim sh As Worksheet Dim i As Long Dim nm As String Dim nmIdx As Variant Dim FirstDataRow As Long FirstDataRow = 4 Set sh = ActiveSheet With sh Dat = Range(.Cells(FirstDataRow, "B"), .Cells(.Rows.Count, "A").End(xlUp)) Set PriceList = Range(.Cells(FirstDataRow, "E"), .Cells(.Rows.Count, "F").End(xlUp)) PriceListNames = Application.Transpose(PriceList.Columns(1)) ' Need a 1D array for Match PriceListPrices = PriceList.Columns(2) ReDim Res(1 To UBound(Dat, 1), 1 To 1) For i = 1 To UBound(Dat, 1) nm = Dat(i, 1) nmIdx = Application.Match(nm, PriceListNames, 0) If Not IsError(nmIdx) Then Res(i, 1) = Dat(i, 2) * PriceListPrices(nmIdx, 1) End If Next Range(.Cells(FirstDataRow, 3), .Cells(UBound(Dat, 1) + FirstDataRow - 1, 3)) = Res End With End Sub 

你可以用C列中的一个简单的VLOOKUP公式来做到这一点。

 =VLOOKUP(A1,E1:F65000,2,FALSE)*B1 

您也可以在列E和列F中使用命名范围,因此您不必依赖像E1:F65000这样的固定地址。

尝试这个:

 Sub HTH() With Worksheets("Sheet1") With .Range("A4", .Range("A" & Rows.Count).End(xlUp)).Offset(, 2) .Formula = "=VLOOKUP(A4,E:F,2,FALSE)*$B$1" .Value = .Value End With End With End Sub