2017-01-31 55 views
1

我已成功編寫了一個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

1.你可以保持一個獨立的行計數器爲您導出行2.可以使用.END(xlUp)(谷歌如何找到最後一排一列),但請注意,通過這樣做,您將失去檢測是否已複製這些行的能力。如果你用#2運行宏兩次,那麼你會得到重複。如果您使用行計數器進行導出,那麼您可以根據需要覆蓋多次。 –

回答

1

你可以設置一個變量爲目標行是這樣的:

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 

destiny_row = 2 
For x = 2 To MaxRowList 
    If InStr(1, wsSource.Cells(x, 1), "2016") Then 
    wsTarget.rows(destiny_row).Value = wsSource.rows(x).Value 
    destiny_row = destiny_row +1 
    End If 
Next 

Application.ScreenUpdating = True 

End Sub 

通過這種方式,它會開始複製目的地表第2行的值,並根據若condition.Tell我如何增加它會...

+0

這很好用!非常感謝你。你可以幫我進一步嗎?我需要將完整的第一行從工作表1複製到工作表2的第1行。謝謝。 :) –

+0

很高興它有幫助,當然,你只需要使用「wsSource.rows(x).EntireRow.Copy」,然後在目標中使用.paste。請記住標記我的答案是正確的,如果它幫助你。 – jsanchezs

0
Sub CopyRow() 

    Application.ScreenUpdating = False 

    Dim x As Long 
    Dim MaxRowList As Long, PrintRow 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 
      PrintRow = wsTarget.range("A" & wsTarget.rows.count).end(xlup).row 
      wsTarget.rows(PrintRow).Value = wsSource.rows(x).Value 
     End If 
    Next 

    Application.ScreenUpdating = True 

End Sub 
+0

由於某些原因,它沒有複製在列A中包含2016的所有行。它僅複製一個。在我的工作表中,我在A列中有2個2016年的條目(他們在A3和A7中)。 –

1

你可以使用AutoFilter方法,這將節省您需要通過所有的行使用For循環,只是整個過濾範圍複製到「選項卡2」工作表。

代碼(內部意見的說明)

Option Explicit 

Sub CopyRow() 

Application.ScreenUpdating = False 

Dim x As Long 
Dim MaxRowList As Long 
Dim MaxCol As Long 

Dim S As String 
Dim aCol As Long 
Dim wsSource As Worksheet 
Dim wsTarget As Worksheet 
Dim SourceRng As Range 
Dim VisRng As Range 
Set wsSource = ThisWorkbook.Worksheets("Tab 1") 
Set wsTarget = ThisWorkbook.Worksheets("Tab 2") 

aCol = 1 

With wsSource 
    MaxRowList = .Cells(.Rows.Count, aCol).End(xlUp).Row ' find last row 
    MaxCol = .Cells(1, .Columns.Count).End(xlToLeft).Column ' find last column 

    Set SourceRng = .Range(.Cells(1, 1), .Cells(MaxRowList, MaxCol)) ' set source range to actually occupied range 

    .Range("A1").AutoFilter ' use AutoFilter method 
    SourceRng.AutoFilter Field:=1, Criteria1:="2016" 

    Set VisRng = SourceRng.SpecialCells(xlCellTypeVisible) ' set range to filterred range 

    VisRng.Copy ' copy entire visible range 
    wsTarget.Range("A2").PasteSpecial xlPasteValues ' past with 1 line 
End With 

Application.ScreenUpdating = True 

End Sub 
+0

我在我的工作表上試過這段代碼,但它刪除了第1行以外的工作表1中的所有條目。它對工作表2沒有任何作用。 –

+0

@ D.Todor已刪除?我的代碼中沒有我刪除的地方 –

相關問題