我正在寻找一个marco,它能够搜索所述工作表中的列,如果找到某些文本-在我的例子中是单词"FAIL“-复制整个行的data/formatting并将其粘贴到另一个工作表中-在我的例子中是sheet 4-以及包含该特定文本的任何其他行。
我一直在使用这个代码,但它只复制粘贴一行,然后停止,而不是遍历并复制任何带有"FAIL“的行
Sub Test()
For Each Cell In Sheets(1).Range("H:H")
If Cell.Value = "FAIL" Then
matchRow = Cell.Row
Rows(matchRow & ":" & matchRow).Select
Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Sheets(4).Select
ActiveSheet.Rows(matchRow).Select
ActiveSheet.Paste
Sheets(4).Select
End If
Next
End Sub
VBA的第一篇帖子和全新的文章,如果太含糊了,很抱歉。
发布于 2017-07-20 16:46:06
尝试下面的代码(代码中的解释作为注释):
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
发布于 2017-07-20 16:34:46
试着这样做:
Option Explicit
Sub TestMe()
Dim Cell As Range
Dim matchRow As Long
With Worksheets(1)
For Each Cell In .Range("H:H")
If Cell.Value = "FAIL" Then
matchRow = .Cell.Row
.Rows(matchRow & ":" & matchRow).Select
.Rows(matchRow & ":" & matchRow).Select
Selection.Copy
Worksheets(4).Select
Worksheets(4).Rows(matchRow).Select
Worksheets(4).Paste
.Select
End If
Next
End With
End Sub
代码中的问题是您没有始终正确地引用工作表。因此,它不能正常工作。
作为2.步骤,您可以尝试避免代码中的所有选择,最佳做法是避免在Excel VBA中使用Select或Activate。
https://stackoverflow.com/questions/45209392
复制相似问题