繁体   English   中英

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

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

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

我想创建一个 VBA 以在 A 列的主工作表“NIR”中搜索,并根据主工作表“NIR”中的单元格将 B 列和 C 复制到特定工作表中。

例子:

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

根据表“NIR” A3B3C3复制到表“鞋”和“裤子”

尝试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM