以下代码正在跳过第一个" hit"。在" Notes"中," x"在G列中将粘贴到新工作表。但是,第一行带有" x"每次被排除在外:
Sub Test()
For Each Cell In Sheets("Notes").Range("G:G")
If Cell.Value = "x" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Copy
Sheets("Completed Conversions").Select
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
ActiveSheet.Paste
Sheets("Notes").Select
End If
Next
Sheets("Completed Conversions").Select
End Sub
谢谢!
答案 0 :(得分:2)
需要在工作表之间切换Select
才能从一个工作表复制到另一个工作表。
其次,如果要复制整行,而不是:
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Copy
使用:
Cell.EntireRow.Copy
最后一件事:不是循环遍历整个列G,而是循环遍历其中包含值的所有单元格:
For Each Cell In Sheets("Notes").Range("G1:G" & Sheets("Notes").Cells(Sheets("Notes").Rows.Count, "G").End(xlUp).Row)
您的整个代码可以替换为以下代码:
Sub Test()
Dim Cell As Range
With Sheets("Notes")
For Each Cell In .Range("G1:G" & .Cells(.Rows.Count, "G").End(xlUp).Row)
If Trim(Cell.Value) = "x" Then
Cell.EntireRow.Copy Destination:=Sheets("Completed Conversions").Range("A" & Sheets("Completed Conversions").Rows.Count).End(xlUp).Offset(1)
End If
Next Cell
End With
End Sub