如果列“I”包含某个字母(或短语),我正在尝试将行数据从一个工作表复制到另一个工作表。例如,我要复制的数据位于名为“Clients”的工作表上,如果第I列包含字母“T”,那么我希望该数据能够自动导入到下一个名为“Tax-Pending”的工作表中但我遇到了问题。我还可以考虑更改第I列中的文本,以便在必要时更具描述性。我的数据从所有工作表的第3行开始(前两行是标题)。
Sub CopyRow()
Dim x As Long
Dim iCol As Integer
Dim MaxRowList As Long
Dim S As String
Set wsSource = Worksheets("Clients")
Set wsTarget = Worksheets("Tax - Pending")
iCol = 1
MaxRowList = wsSource.Cells(Rows.Count, iCol).End(xlUp).Row
For x = MaxRowList To 1 Step -1
S = wsSource.Cells(x, 1)
If S = "T" Then
AfterLastTarget = wsTarget.Cells(Rows.Count, 1).End(xlUp).Row + 1
wsSource.Rows(x).Copy
wsTarget.Rows(AfterLastTarget).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
wsSource.Rows(x).Delete
End If
Next
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:5)
如果您不需要格式化,这应该有效。请注意,这将"复制"列I 包含字母" T"的任何行。换句话说," 1T"," AT"," TT"," @ T"或任何包括" T"将被复制。
Sub CopyRow()
Application.ScreenUpdating = False
Dim x As Long
Dim MaxRowList As Long
Dim S As String
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Set wsSource = ThisWorkbook.WorkSheets("Clients")
Set wsTarget = ThisWorkbook.WorkSheets("Tax - Pending")
iCol = 1
MaxRowList = wsSource.Cells(Rows.Count, iCol).End(xlUp).Row
For x = 3 To MaxRowList
If InStr(1, wsSource.Cells(x, 9), "T") Then
wsTarget.Rows(x).Value = wsSource.Rows(x).Value
End If
Next
Application.ScreenUpdating = True
End Sub
如果你只想要" T"而没有其他,然后使用
If wsSource.Cells(x, 9) = "T" Then
修改:如果您要将数据附加到目标表的底部,请将if
块更改为
If InStr(1, wsSource.Cells(x, 9), "T") Then
AfterLastTarget = wsTarget.Cells(Rows.Count, 1).End(xlUp).Row
wsTarget.Rows(AfterLastTarget + 1).Value = wsSource.Rows(x).Value
End If