将来自多个工作簿的数据合并到主数据库中

时间:2014-03-11 16:51:36

标签: excel vba excel-vba

我一直在搜索论坛,但我的代码无法解决问题。我是非常新的宏,我确信它很简单,就像一些未定义的变量,但我无法弄明白。 我正在尝试将多个工作簿中的数据加载到主服务器中,真的需要帮助!

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

谢谢!

1 个答案:

答案 0 :(得分:0)

要减少错误,您应该在模块顶部说明Option Explicit。然后,您将被告知何时使用未声明的变量,并降低拼写错误变量名称的风险。

您应该将SumName = Dir(SumPath & SumTemplate)置于循环之前,因为Dir末尾的Do While ... Loop将引用具有参数的最后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