我已成功編寫了一個Excel VBA腳本,用於檢查列A是否包含特定條目(本例中爲2016),然後將整行復制到一個新的工作表。在Excel中將空行復制到使用VBA的新工作表中後刪除空行
唯一的問題是它將行復制到與原始工作表完全相同的位置。正因爲如此,我得到了空行之間。我希望宏在複製之後立即刪除這些空行,或者將這些行一個接一個地複製到新的工作表中。
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("Tab 1")
Set wsTarget = ThisWorkbook.Worksheets("Tab 2")
aCol = 1
MaxRowList = wsSource.Cells(rows.Count, aCol).End(xlUp).Row
For x = 2 To MaxRowList
If InStr(1, wsSource.Cells(x, 1), "2016") Then
wsTarget.rows(x).Value = wsSource.rows(x).Value
End If
Next
Application.ScreenUpdating = True
End Sub
任何幫助表示讚賞。提前致謝。
1.你可以保持一個獨立的行計數器爲您導出行2.可以使用.END(xlUp)(谷歌如何找到最後一排一列),但請注意,通過這樣做,您將失去檢測是否已複製這些行的能力。如果你用#2運行宏兩次,那麼你會得到重複。如果您使用行計數器進行導出,那麼您可以根據需要覆蓋多次。 –