Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

excel - How to copy a row if it contains certain text to the next available line in another worksheet

I'm looking to modify the code below that I received from this forum so that it copies to the next available row on Sheet 5.

Private Sub CommandButton1_Click()
    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 = "YES" Then
                 ' Copy>>Paste in 1-line (no need to use Select)
                .Rows(Cell.Row).Copy Destination:=Sheets(5).Rows(Cell.Row)
            End If
        Next Cell
    End With
End Sub

This following code worked for the copy over to the next available row, however If I kept clicking the commandbutton it would keep pasting the information over and over again. It also did not keep the source formatting and formulas, only copied the text. I don't want it to duplicate in the destination sheet and I would like it to copy over the formatting and formulas

Private Sub CommandButton1_Click()
    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 = "Matt Stephens" Then
            .Range("A" & Cell.Row & ":Q" & Cell.Row).Copy
             ' Paste as Links requires to select a destination cell
            Sheets(5).Range("A" & Sheets(5).Cells(Sheets(5).Rows.Count, "A").End(xlUp).Row + 1).Select
            ActiveSheet.Paste Link:=True
        End If
    Next Cell
    End With
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try the below:

Private Sub CommandButton1_Click()
Dim Cell As Range
Dim LastRow As Long

Application.ScreenUpdating = False

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 = "YES" And Sheets(1).Range("R" & Cell.Row) <> "Copied" Then
            ' add "Copied" flag so row is not duplicated again
            Sheets(1).Range("R" & Cell.Row).Value = "Copied"
            ' Copy>>Paste in 1-line (no need to use Select)
            LastRow = Sheets(5).Cells(Sheets(5).Rows.Count, "A").End(xlUp).Row + 1
            .Range("A" & Cell.Row & ":Q" & Cell.Row).Copy
            Sheets(5).Rows(LastRow).PasteSpecial xlValues
            Sheets(5).Rows(LastRow).PasteSpecial xlFormats
        End If
    Next Cell
End With

Application.ScreenUpdating = True
Sheet1.Select

End Sub

You need to use PasteSpecial to copy formats, as well as use the LastRow variable to paste into the next available cell and not overwrite previously copied data.

Also, unless you have a unique identifier to check if a cell already exists on the copied sheet, you would need to use a simple flag, as I have done in the above to mark the cell as copied. I am marking the word "Copied" in each row that has been copied after your data in column R. You can move that to any other column or hide it if you don't want it to be visible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...