这不是一个真正有效的代码,但它应该让您很好地了解如何使用循环(列、行、数组)
注意:如果您的电子表格中有某种结构(例如标题),那么您可以找到行中最后使用的列 - 这比遍历工作表中的所有列更有效。
Sub doTheJob()
Application.ScreenUpdating = False
Dim ws1 As Worksheet, ws2 As Worksheet, arr() As String, str$, i&, j&, k&
Set ws1 = ThisWorkbook.Sheets(1)
Set ws2 = ThisWorkbook.Sheets(2)
str = "this is your string"
arr = Split(str, " ")
For i = 1 To ws1.Columns.Count
For j = 1 To ws1.Cells(Rows.Count, i).End(xlUp).Row
For k = LBound(arr) To UBound(arr)
If StrComp(CStr(ws1.Cells(j, i).Value), arr(k), vbTextCompare) = 0 Then
ws1.Rows(j & ":" & j).Select
Selection.Copy
Dim nxt&
nxt = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1
ws2.Rows(nxt & ":" & nxt).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next k
Next j
Next i
Application.ScreenUpdating = True
End Sub
后编辑 - 它现在应该复制整行......