我需要在某些地方合并的列中搜索特定单词,如果存在,我需要复制也合并的行并将其粘贴到不同的工作表上。
下面的代码我只使用复制和粘贴包含单词的第一个合并行,然后它给我一个错误。它应该遍历整个工作表并复制包含该单词的所有行。
我已经评论了我的代码,所以它更容易理解。
Sub SearchForString()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
On Error GoTo Err_Execute
'Start search in row 10
LSearchRow = 11
'Start copying data to row 1 in Sheet12 (row counter variable)
LCopyToRow = 1
While Len(Range("A" & CStr(LSearchRow)).Value) <> Null
'If value in column E = "ENGINE AUXILIARY PANEL (EAP 1)", copy entire row to Sheet12
If Range("E" & CStr(LSearchRow)).Value = "13.8kV SWITCHGEAR METERING CELL #A1 (+06)" Then
'Select row in Sheet1 to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into Sheet2 in next row
Sheets("Sheet14").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
'Move counter to next row
LCopyToRow = LCopyToRow + 1
'Go back to Sheet1 to continue searching
Sheets("Sheet11").Select
End If
LSearchRow = LSearchRow + 1
Wend
Exit Sub
Err_Execute: