【问题标题】:Excel: How to use Copy Paste command to selected sheet area (VBA)Excel:如何使用复制粘贴命令到选定的工作表区域(VBA)
【发布时间】:2017-12-27 02:16:28
【问题描述】:

扩展问题来自- Excel: How to copy a row if it contains certain text to another worksheet (VBA)

我希望修改下面的代码,以便它能够从表 2 复制到表 4,粘贴相邻 (L:U)?看图片。

pic2

Option Explicit

Sub Test()

Dim Cell As Range

With Sheets(1)
' loop column H untill last cell with value (not entire column)
For Each Cell In .Range("H1:H" & .Cells(.Rows.Count, "H").End(xlUp).Row)
    If Cell.Value = "FAIL" Then
         ' Copy>>Paste in 1-line (no need to use Select)
        .Rows(Cell.Row).Copy Destination:=Sheets(4).Rows(Cell.Row)
    End If
Next Cell
End With

End Sub

评论回复图片pic3

【问题讨论】:

    标签: vba excel copy-paste


    【解决方案1】:

    像这样?

    Option Explicit
    
    Sub Test()
    
    Dim Cell As Range
    
    With Sheets(1)
    ' loop column H untill last cell with value (not entire column)
    For Each Cell In .Range("H1:H" & .Cells(.Rows.Count, "H").End(xlUp).Row)
        If Cell.Value = "FAIL" Then
             ' Copy>>Paste in 1-line (no need to use Select)
            .Range("A" & Cell.Row & ":J" & Cell.Row).Copy Destination:=Sheets(4).Range("L" & Sheets(4).Cells(Sheets(4).Rows.Count, "L").End(xlUp).Row + 1)
        End If
    Next Cell
    End With
    
    End Sub
    

    复制链接的宏:

    Option Explicit
    
    Sub Test()
    
    Dim Cell As Range
    
    With Sheets(1)
    ' loop column H untill last cell with value (not entire column)
    For Each Cell In .Range("H1:H" & .Cells(.Rows.Count, "H").End(xlUp).Row)
        If Cell.Value = "FAIL" Then
            .Range("A" & Cell.Row & ":J" & Cell.Row).Copy
             ' Paste as Links requires to select a destination cell
            Sheets(4).Range("L" & Sheets(4).Cells(Sheets(4).Rows.Count, "L").End(xlUp).Row + 1).Select
            ActiveSheet.Paste Link:=True
        End If
    Next Cell
    End With
    
    End Sub
    

    【讨论】:

    • 有什么诀窍,但是当我点击两次抓取 marco 时,它只会在下面粘贴相同的数据?
    • 我添加了一张结果图片(pic3)
    • @Hufflegigans 每次运行此宏时,它都会复制所有数据。我添加了另一个解决方案。它将复制指向您的源数据的链接,因此无需更新它(我无法在 ATM 上测试它,希望它能正常工作)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多