2016-04-24 46 views
2

我是VBA的新手。我一直在尋找小時,但無濟於事:(將多個工作表中的特定列複製到一箇中

我有12張由AT列組成的工作表我想從12張工作表中的每一張複製併合並C和T列到一個工作簿「摘要」中宏。有人能幫助我嗎?在此先感謝。

Sub Create_Summary() 

Application.DisplayAlerts = False 
On Error Resume Next 

Application.DisplayAlerts = True 
n = Application.Worksheets.Count 

Sheets("Summary").Move after:=Worksheets(Worksheets.Count) 

Dim sh As Worksheet 
For Each sh In ActiveWorkbook.Worksheets 
    If sh.Name <> "Summary" Then 
    Set col = Columns(Columns.Count).End(xlToLeft) 
     Set col = Columns(Columns.Count).End(xlToLeft) 
     sh.Range("C:C,T:T").Copy Destination:=Sheets("Summary").Range(col,col) 
    End If 

Next sh 

End Sub 

此副本C和T列從最後一張紙,而其他人只是C時不T.

+0

你有沒有嘗試錄製宏? – OldUgly

+0

請告訴我們你到目前爲止嘗試了些什麼。 –

+0

我已經添加了代碼,希望你能幫上忙。 – znf96

回答

1

試試這個

Option Explicit 

Sub Create_Summary() 
Dim sh As Worksheet, sumSht As Worksheet 
Dim i As Long 

Set sumSht = Sheets("Summary") 
sumSht.Move after:=Worksheets(Worksheets.Count) 

For i = 1 To Worksheets.Count - 1 ' once you moved "Summary" sheet as the workbook last one, you skip it by limiting loop to the penultimate sheets index 
    Worksheets(i).Range("C:C,T:T").Copy Destination:=sumSht.Cells(1, sumSht.Columns.Count).End(xlToLeft).Offset(, 1) ' qualify all destination references to "Summary" sheet 
Next i 
sumSht.Columns(1).Delete ' "Summary" sheet first column gest skipped by the above loop, so delete it 

End Sub 

它已被評論,這樣你就可以按照它來做你的改變

+0

非常感謝你!它的工作:) – znf96

+0

不客氣。如果我完成了您的問題,請將答案標記爲已接受。謝謝 – user3598756

相關問題