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

回答ありがとうございます。これが私の最終結果です:

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 に答える
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

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 に答える