Excel 根据单元格值在特定 sheet 上复制粘贴范围

Excel copy-paste range on specific sheet according to cell value

我有一个名为“NIR”的主 sheet 工作簿,其中包含以下数据:第 A 列包含产品名称(与其余工作相同sheets names);列 B 包含数量,列 C 包含价格。

我想创建一个 VBA 来在我的 master sheet A 列中搜索“NIR”,并根据 master sheet "NIR",A 列中的单元格。

示例:

Sheet "NIR"
A3="shoes"
A4="pants"
B3 = 3 (pairs)
C3 = 10 (price)

根据Sheet将B3C3复制到sheet的“鞋子”和“裤子” "近红外" A3

尝试:

Option Explicit

Sub Macro1()

    Dim LastRowNIR As Long, i As Long, LastRowWs As Long
    Dim arr As Variant
    
    With ThisWorkbook.Worksheets("NIR")
        'Find the last row of column A
         LastRowNIR = .Cells(.Rows.Count, "A").End(xlUp).Row
         
         'Import all data in an array starting from A1 to C last row
         arr = .Range("A1:C" & LastRowNIR)
         'Loop array
         For i = LBound(arr) To UBound(arr)
            
            With ThisWorkbook.Worksheets(arr(i, 1))
                'Find the last row of column B
                LastRowWs = .Cells(.Rows.Count, "B").End(xlUp).Row
                'Write in the next available row the quantity
                .Range("B" & LastRowWs + 1).Value = arr(i, 2)
                'Write in the next available row the prices
                .Range("C" & LastRowWs + 1).Value = arr(i, 3)
            End With
            
         Next i
         
    End With
    
End Sub