【问题标题】:Excel copy-paste range on specific sheet according to cell valueExcel根据单元格值在特定工作表上复制粘贴范围
【发布时间】:2020-08-06 08:09:43
【问题描述】:

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

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

例子:

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

根据Sheet“NIR”A3

B3C3复制到床单“shoes”和“pants”

【问题讨论】:

    标签: excel vba copy paste worksheet


    【解决方案1】:

    试试:

    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
    

    【讨论】:

    • 它可以工作,值很好,它粘贴到正确的工作表上。只是 1 个小问题,它粘贴在表格的底部。我忘了指定表格标题出现在第 1 行和第 2 行, 数据应该从第 3 行开始粘贴。你能告诉我这个公式中我需要更正什么吗?非常感谢!
    • 那么,每次你都会在第3行添加?
    • arr = .Range("A3:C" & LastRowNIR)替换arr = .Range("A1:C" & LastRowNIR)
    • 我试过了,但它仍然粘贴在桌子的底部
    • LastRowWs = .Cells(.Rows.Count, "B").End(xlUp).Row 'Write in the next available row the quantity .Range("B" & LastRowWs + 1).Value = arr(i, 2) 是否可以将某些内容更改为.End(xlDown). 或使用偏移值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多