簡體   English   中英

將多個工作簿中的數據合並到一個母版中

[英]Combining data from multiple workbooks into a master

我一直在搜索論壇,但無法解決我的代碼問題。 我對宏非常陌生,我敢肯定這很簡單,例如未定義某些變量,但我無法弄清楚。 我正在嘗試將多個工作簿中的數據加載到母版中,請確實需要幫助!

Dir for source files: C:\Test Dir\
Dir for Master: C:\Test Dir\Master\

源文件名不同,但所有文件名均以“ * FORMATTED.xlsx”結尾。

主文件名: "Payroll Master.xlsx"

源工作表名稱=“已加載的數據”

主工作表名稱=“摘要”

所有SOURCE數據都在A2:J106中。

源文件和主文件中的第一行是列標題,並且相同。 我正在將所有數據加載到主文件“摘要”工作表中。

我的最新錯誤是: "Run-time error '1004': Select method of Range class failed." "Sheets("Loaded Data").Range("A2:J106").Select"行上

這是我當前的代碼:

Sub combine_data()
'
Dim MyPath As String
Dim SumPath As String
Dim MyName As String
Dim SumName As String
'Dim MyTemplate As Workbook
'Dim SumTemplate As Workbook
MyPath = "C:\Test Dir\"
SumPath = "C:\Test Dir\Master\"
MyTemplate = "*.xlsx"  'Set the template.
SumTemplate = "Payroll MASTER.xlsx"
MyName = Dir(MyPath & MyTemplate)    'Retrieve the first file
SumName = Dir(SumPath & SumTemplate)

Do While MyName <> ""
    Workbooks.Open MyPath & MyName
Sheets("Loaded Data").Range("A2:J106").Select
Selection.Copy
    Workbooks.Open SumPath & SumName
Sheets("Summary").Select
Range("A65536").End(xlUp).Offset(1, 0).Activate
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks(MyName).Close SaveChanges:=False        'close
Workbooks(SumName).Close SaveChanges:=True
MyName = Dir                    'Get next file
Loop
End Sub

謝謝!

為了減少錯誤,您應該在模塊頂部聲明Option Explicit 當使用未聲明的變量時,系統會提示您,這樣可以降低拼寫變量名稱的風險。

您應該將SumName = Dir(SumPath & SumTemplate)放在循環之前,因為Do While ... Loop末尾的Dir將引用具有參數的LAST Dir 當通過您描述的選擇克服錯誤時,您就遇到了這個問題。

在循環中,您應該單獨參考每個工作簿/工作表,以闡明您的工作(為將來的發展提供幫助)。

您正在打開和關閉每個源文件的MASTER文件。 您可以在循環之前將其打開,然后在循環之后將其關閉。 這將使您的腳本更快。

這是用上面的注釋修改的代碼:

Option Explicit

Sub combine_data()
'
Dim MyPath As String
Dim SumPath As String
Dim MyName As String
Dim SumName As String
Dim MyTemplate As String
Dim SumTemplate As String
Dim myWS As Worksheet
Dim sumWS As Worksheet

'Define folders and filenames
MyPath = "C:\Test Dir\"
SumPath = "C:\Test Dir\Master\"
MyTemplate = "*.xlsx"  'Set the template.
SumTemplate = "Payroll MASTER.xlsx"

'Open the template file and get the Worksheet to put the data into
SumName = Dir(SumPath & SumTemplate)
Workbooks.Open SumPath & SumName
Set sumWS = ActiveWorkbook.Worksheets("Summary")

'Open each source file, copying the data from each into the template file
MyName = Dir(MyPath & MyTemplate)    'Retrieve the first file
Do While MyName <> ""
    'Open the source file and get the worksheet with the data we want.
    Workbooks.Open MyPath & MyName
    Set myWS = ActiveWorkbook.Worksheets("Loaded Data")
    'Copy the data from the source and paste at the end of Summary sheet
    myWS.Range("A2:J106").Copy
    sumWS.Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
    'Close the current sourcefile and get the next
    Workbooks(MyName).Close SaveChanges:=False        'close
    MyName = Dir                    'Get next file
Loop
'Now all sourcefiles are copied into the Template file. Close and save it
Workbooks(SumName).Close SaveChanges:=True
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM