代码之家  ›  专栏  ›  技术社区  ›  Hufflegigans

Excel:如何使用“复制粘贴”命令到选定图纸区域(VBA)

  •  0
  • Hufflegigans  · 技术社区  · 7 年前

    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

    1 回复  |  直到 7 年前
        1
  •  0
  •   Egan Wolf    7 年前

    这样地?

    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