【问题标题】:Copying worksheet data from multiple workbooks and pasting it into a master data file by worksheet从多个工作簿复制工作表数据并按工作表将其粘贴到主数据文件中
【发布时间】:2016-03-31 23:25:17
【问题描述】:

我是 VBA 的新手,但是我被赋予了使用 VBA 完成的任务。如何创建一个代码,通过将完全相同数量的单独工作表添加到此主数据文件中,从不同工作簿复制多个工作表的数据并将它们粘贴到另一个工作簿(主数据文件)?也就是说,我想在主数据文件中显示所有这些工作表被复制到单独的工作表中。

我已经设法提取了一个代码,该代码将数据复制并粘贴到一个工作表中,但我正在努力将它们一一复制到单独的工作表中。

非常感谢您的帮助。

Sub datatransfer()

    Dim FolderPath, FilePath, Filename, targetfile As String
    Dim wb1, wb2 As Workbook
    Dim i, mycount As Long

    targetfile = "Left the location out on purpose"
    FolderPath = " Left the location out on purpose "
    FilePath = FolderPath & "*.xls*"

    Filename = Dir(FilePath)

    Dim lastrow, lastcolumn As Long

    Do While Filename < ""

        mycount = mycount + 1

        Filename = Dir()

        Set wb1 = Workbooks.Open(FolderPath & Filename)

        lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

        lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column

        Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy

        Application.DisplayAlerts = False

        Set wb2 = Workbooks.Open(targetfile)

        Worksheets.Add Before:=Sheet1, Count:=2


        For i = 1 To mycount

            With Worksheets(i)

                ActiveSheet.Paste Destination:=.Range(Cells(2, 2), Cells(2, lastcolumn))

            End With

        Next i

        ActiveWorkbook.Close SaveChanges:=True

        Filename = Dir

    Loop

End Sub

【问题讨论】:

    标签: vba excel macros


    【解决方案1】:

    请参阅下面的代码。我做了一些笔记,我对代码进行了一些修改,以确保它能够在未来顺利运行。

    Sub datatransfer()
    
        'have to specify type for all variables, techinically it still works the way you did, but you are setting unncessary memory
        Dim FolderPath As String, FilePath As String, Filename As String, targetfile As String
        Dim wb1 As Workbook, wb2 As Workbook
    
        targetfile = "Left the location out on purpose"
        FolderPath = " Left the location out on purpose "
        FilePath = FolderPath & "*.xls*"
    
        Set wb2 = Workbooks.Open(targetfile) 'only need to open this once and leave open until execution is finished
    
        Filename = Dir(FilePath)
    
        Do While Filename <> "" ' need "<>" to say not equal to nothing
    
            wb2.Worksheets.Add After:=wb2.Worksheets(wb2.Worksheets.Count) 'add new sheet to paste data in target book
    
            Set wb1 = Workbooks.Open(FolderPath & Filename)
    
            Dim lastrow As Long, lastcolumn As Long
    
            With wb1.Worksheets(1) 'best to qualify all objects and work directly with them
                lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
                lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
    
                'pretty sure you want to add this A1, since it's a new blank sheet
                .Range(.Cells(2, 1), .Cells(lastrow, lastcolumn)).Copy _
                    Destination:=wb2.Worksheets(wb2.Worksheets.Count).Range("A1")
    
            End With
    
            wb1.Close False 'assume no need to save changes to workbook you copied data from
    
            Filename = Dir
    
        Loop
    
        wb2.Close True 'no close and save master file
    
    End Sub
    

    【讨论】:

    • 斯科特,感谢您抽出宝贵时间。 “ wb2.Worksheets.Add After:=wb.Worksheets.Count ”位如何考虑到我想添加等于文件数的工作表?如果我修改为“wb1.Worksheets.Count”,则会弹出运行时错误 91 窗口。在阶段 wb2 仍未初始化。我从中复制数据的每个文件仅包含一个工作表,因此文件夹中的文件数等于要添加的工作表数。修改后的代码如何知道文件夹路径中的文件数?斯科特,非常感谢。
    • @Richard - 首先该行应该是wb2.Worksheets.Add After:=wb2.Worksheets.Count。对不起,语法错误。 wb2 在循环开始之前就被初始化了,这很好。我的代码从不查找或找出路径中的文件数,而是为每次循环处理和循环添加一个工作表 (wb2.Worksheets.Add ... 行)遍历路径中的每个.xls 文件,为每个文件添加一个新工作表。
    • 如果我将其初始化为 wb2,则会出现运行时错误 1004。它是 MS Office 2010。即使存在现有的空白工作表,它也不会将每张工作表复制到空白的新工作表中。它将源数据仅转储到一个工作表上。即使有很多源文件,您是否建议我限定所有对象?我们可能需要从大量文件中复制数据,这就是我想到With 结构的原因。非常感谢,斯科特。
    • @Richard 我又做了一个编辑。语法仍然关闭。对于那个很抱歉。此行Destination:=wb2.Worksheets(wb2.Worksheets.Count).Range("A1") 将复制到 wb2 中的最后一个工作表 - 这是为该特定文件创建的新添加的工作表。 (我刚刚在我的系统上完全测试了这段代码,它可以工作)。
    • 它也适用于我的系统。我非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多