2011-02-17 159 views
0

首先我爲我試圖做的是一個新手,以VBA但僞代碼:幫助VBA腳本

For All open Excel Files 
Copy all values in Colomns A,B, C and D 
Append into Tab 1 of output.xls 

我將不勝感激一些指引了正確的方向。

感謝

+1

您是否嘗試過錄制宏以獲取指針? – Fionnuala 2011-02-17 22:03:02

回答

2

有時候,最好的學習方法是錄製宏。

工具>宏 - 選擇記錄。

然後進入你的工作簿,選擇列A,B,C,D然後CTRL + C,然後打開你的新的TaB和CTRL + V.

停止錄製宏,然後ALT + F11查看生成的代碼,這應該給你一個啓動十。

如果您需要幫助瞭解生成的代碼/它做了什麼回來,我們可以解釋。

+0

感謝隊友,試過了,我現在看到這個工具可以有多大幫助。 – 2011-02-17 22:24:59

2

雖然有一些錄製宏的東西不會幫助你,例如,使用For ... Each來遍歷工作簿中的每個工作表。以下是一些示例代碼,以便您指出正確的方向。這將迭代所有打開的工作簿,並將前四列的內容複製到工作表上。

Sub joinAllSheets() 
Dim ws As Worksheet 
Dim wb As Workbook 
Dim wsOutput As Worksheet 
Dim lngRowCount As Long 
Dim wbSource As Workbook 

'create output workbook 
Set wsOutput = Application.Workbooks.Add.Sheets(1) 
lngRowCount = 1 

'Iterate through each open workbook 
For Each wb In Application.Workbooks 

'if the current workbook is not our output workbook then 
If wb.Name <> wsOutput.Name Then 
    'iterate through each worksheet 
    For Each ws In wb.Worksheets 
    'copy the first four columns of the used range in the worksheet 
    Application.Intersect(ws.UsedRange, ws.Range("A:D")).Copy _ 
      Destination:=wsOutput.Cells(lngRowCount, 1) 
    'we need to count how many rows there are in the usedrange so we know 
    'where to paste into the output worksheet 
    lngRowCount = lngRowCount + ws.UsedRange.Rows.Count + 1 
    Next ws 

End If 

Next wb 

End Sub