0

我有

A 列 - 名称
B 列 - 数量
C 列 - 我想要返回数量 x 成本的值
E 列 - 名称但位于不同的单元格
F 列 - 价格

我想要实现的是:从A1中获取值并在E:E中找到它(假设我们在E10中找到它),当找到时获取B1的值并将其乘以F10的相应值- 并将所有这些放入C栏

对于A 列中的所有值,依此类推

我试图用 Do while 和两个变量 x 和 y 来做到这一点,但由于某种原因,它不能只找到某些行的所有值。

先感谢您。

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
4

3 回答 3

2

VLOOKUP您可以使用C 列中的简单公式来执行此操作。

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

您还可以为 E 和 F 列中的数据使用命名范围,因此您不必依赖像E1:F65000.

于 2013-10-18T20:44:45.800 回答
1

要使用 VBA 执行此操作,您应该将源数据复制到 Variant 数组并循环遍历这些数组。速度更快,IMO 更易于阅读和调试。

像这样的东西

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
于 2013-10-18T20:59:20.230 回答
1

试试这个:

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
于 2013-10-18T21:03:55.937 回答