【问题标题】:VBA - Finding & Changing values across sheetsVBA - 跨工作表查找和更改值
【发布时间】:2012-10-10 00:55:48
【问题描述】:

晚上好!一直在搜索,恐怕我刚刚得到了一些完全倒退的东西(我对 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 中执行此操作。感谢阅读:)

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    我们看到 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

    【讨论】:

    • 太棒了!谢谢你们的回复,我能够使用匹配和索引功能来解决这个问题!然而,我随后销毁了我所有试图使用复制/保存/打印/关闭功能的工作-_-回到空白的工作表绘图板,叹息..哈哈。
    • 我将更新我的帖子以包含我的最终解决方案......一旦我重新创建它
    【解决方案2】:

    我建议你先保存,但试试这个:

    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
    

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

    【讨论】:

      【解决方案3】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-08
        • 1970-01-01
        • 2021-09-30
        • 2023-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多