簡體   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