VBA – 查找和更改表单中的值

晚上好! 一直在寻找,恐怕我刚刚得到了一些完全倒退的东西(我不太了解Excel / VBA,并且99%确定我正在尽一切可能的方式做到最困难的事情)。 我想要做的是从另一张表中find一个值,然后改变它。

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 

接下来我

如果有人能指出我正确的方向,或告诉我要search什么关键字,任何东西都会非常有帮助…我已经被困在这个好几个小时了 – _ – 我试图findOldQty的地址细胞,但没有成功。 在Excel单元格公式中使用(使用= ADDRESS和= MATCH(?))似乎是可行的,但我不知道在VBA中做这件事。 谢谢阅读 :)

它看到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

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

 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 

如果你不确定这个问题的任何部分,请问:)

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

 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