1

晚上好!一直在搜索,恐怕我刚刚得到了一些完全倒退的东西(我对 Excel/VBA 不太了解,而且 99% 的人确信我正在以最困难的方式做所有事情)。我想做的是从另一张纸上找到一个值,然后改变它。

For i = SORowStart To SORowEnd
'Grabbing this information PID and QTY from my main sheet. 

ProductID = Range("B" & i).Value
ProductQty = Range("A" & i).Value

'I then need to match up PID on another sheet, and modify its Qty
If (Len(ProductID) > 0) Then
    'Finding old quantity, successfully..but how do I change it?? 
    OldQty = Application.WorksheetFunction.VLookup(ProductID, Worksheets("Inventory").Range("List_InventoryTable"), 4, False)
    'MsgBox (ProductID & " - SELLING:" & ProductQty & "/" & OldQty)


    'SO HERES MY PROBLEM LINE! ****************************
    'I've tried with and without the "Set" keyword too. 
    'Set Application.WorksheetFunction.VLookup(ProductID, Worksheets("Inventory").Range("List_InventoryTable"), 4, False).Value = (OldQty - ProductQty)


End If

接下来我

如果有人能指出我正确的方向或告诉我要搜索哪些关键字,那么任何东西都会非常有帮助......我已经为此困扰了几个小时 - _ - 我试图找到 OldQty 的地址细胞,但没有成功。似乎可以在 Excel 单元格公式中执行(使用 =ADDRESS 和 =MATCH (?)),但我不知道在 VBA 中执行此操作。谢谢阅读 :)

4

3 回答 3

0

它看到 VLOOKUP 函数返回一个值而不是范围引用。我会给你一个使用 MATCH 和 INDEX 函数的例子:

Sub test()
    Dim ProductIDs As Range
    Dim OldQty As Range

    Set ProductIDs = Application.WorksheetFunction.Index(Sheets("Inventory").Range("List_InventoryTable"), 0, 1)

    For i = 1 To 2
        'Grabbing this information PID and QTY from my main sheet.

        ProductID = Range("B" & i).Value
        ProductQty = Range("A" & i).Value

        'I then need to match up PID on another sheet, and modify its Qty
        If (Len(ProductID) > 0) Then
            'Finding old quantity, successfully..but how do I change it??
            'MsgBox ProductID
            RowIndex = Application.WorksheetFunction.Match(ProductID, ProductIDs, 0)

            Set OldQty = Application.WorksheetFunction.Index(ProductIDs, RowIndex, 1).Offset(0, 3)
            'OldQty = Application.WorksheetFunction.VLookup(ProductID, Sheets("Inventory").Range("List_InventoryTable"), 4, False)
            MsgBox (ProductID & " - SELLING:" & ProductQty & "/" & OldQty)


            'SO HERES MY PROBLEM LINE! ****************************
            'I've tried with and without the "Set" keyword too.
            'Application.WorksheetFunction.VLookup(ProductID, Worksheets("Inventory").Range("List_InventoryTable"), 4, False) = (OldQty - ProductQty)
            OldQty = (OldQty - ProductQty)


        End If
    Next
End Sub

检查此页面http://www.techonthenet.com/excel/formulas/vlookup.php

于 2012-10-08T03:03:22.580 回答
0

我建议你在这样做之前保存,但试试这个:

Sub findstuff()

    Dim currQuantityCell As Range, _
        found As Range, _
        invSheetPIDCol As Range, _
        currQuantityRng As Range, _
        invQuantityRng As Range

    'The range containing your quantities
    Set currQuantityRng = Range("A1:A6")

    'Your PID col inside your range "List_InventoryTable"
    Const PIDCol As Long = 1

    'Your old quantity column inside your range "List_InventoryTable"
    Const oldQuantCol As Long = 4

    Set invSheetPIDCol = Sheets("Inventory").Range("List_InventoryTable").Columns(PIDCol)

    For Each currQuantityCell In currQuantityRng
        If currQuantityCell.Offset(0, 1).Value <> "" Then
            Set found = invSheetPIDCol.find(currQuantityCell.Offset(0, 1).Value, lookat:=xlwhole)
            If Not found Is Nothing Then
                Set invQuantityRng = found.Offset(0, oldQuantCol - PIDCol)
                invQuantityRng.Value = invQuantityRng.Value - currQuantityCell.Value
            Else
                MsgBox "Could not find PID for row " & currQuantityCell.Row
            End If
        End If
    Next currQuantityCell

End Sub

如果您不确定其中的任何部分,请询问:)

于 2012-10-08T03:42:32.497 回答
0

感谢您的回复。这是我的最终结果:

Dim SORowStart As Integer       ' Sales Order - Starting row of line items
Dim SORowEnd As Integer         ' Sales Order - Ending row of line items
SORowStart = Range("OrderRowStart").Value
SORowEnd = Range("OrderRowEnd").Value

Dim ItemToSell As String        ' Sales Order - Selected item PID
Dim QtyToSell As Integer        ' Sales Order - Quantity of item to sell

Dim MatchedIDRow As Integer     ' Inventory sheet - Line # the PID is located on'
Dim Range_InventoryIDs As Range ' Inventory sheet - Range of selectable product IDs
Dim InventoryQty As Range       ' Inventory sheet - Range of selected products Qty

Set Range_InventoryIDs = Application.WorksheetFunction.Index(Sheets("Inventory").Range("List_InventoryTable"), 0, 1)



For i = SORowStart To SORowEnd

    ItemToSell = Range("B" & i).Value
    QtyToSell = Range("A" & i).Value

    If (Len(ItemToSell) > 0) Then
        MatchedIDRow = Application.WorksheetFunction.Match(ItemToSell, Range_InventoryIDs, 0)
        Set InventoryQty = Application.WorksheetFunction.Index(Range_InventoryIDs, MatchedIDRow, 1).Offset(0, 3)

        MsgBox ("Selling " & ItemToSell & " - " & QtyToSell & " / " & InventoryQty.Value)
        InventoryQty.Value = InventoryQty.Value - QtyToSell

        MsgBox ("Remaining " & ItemToSell & " = " & InventoryQty.Value)
    End If
Next i
于 2012-10-09T16:55:48.297 回答