【问题标题】:VBA merge workbooks in a folderVBA 合并文件夹中的工作簿
【发布时间】:2018-02-17 08:27:56
【问题描述】:

我需要将一个文件夹中的所有工作簿合并到一个文件中,我正在寻找的规范是我的文件夹是动态的,所以我需要一个用于代码的文件夹选择器。接下来文件夹中的每个工作簿都有多个工作表,我只需要合并具有名称(“报告”)的工作表。数据也从范围(“A7”)开始。它还包含各个表格中的公式,因此组合后不应出现公式错误。

有人可以帮忙吗?

Sub GetWorkbook()

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False

Path = ThisWorkbook.Path & "\"
Filename = Dir(Path & "*.xlsx")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
ActiveSheet.AutoFilterMode = False
Sheets("Report").Copy After:=ThisWorkbook.Sheets(1)
Range("1:6").EntireRow.Hidden = True
Workbooks(Filename).Close
Filename = Dir()
Loop
Application.DisplayAlerts = True
Combine
Sheets("Home").Select
MsgBox ("Data Consolidated"), vbInformation

End Sub


Function combine
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Combined" And ws.Name <> "Home" Then
ws.Activate
Dim LastRowW As Long
LastRowW = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
ActiveSheet.Range("A7:P" & LastRowW).Copy 
Sheets("Combined").Select
Range("A1048576").End(xlUp).Offset(1, 0).Select
ActiveCell.PasteSpecial (xlPasteValues)
ws.Delete
End If
Next ws

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True

End Function

这是代码,我在宏工作簿中主要有 2 个工作表(组合和家庭)。第二个功能是将详细信息复制到组合工作表并删除其他工作簿。仅当所有工作簿都保存在(此工作簿路径)中时,这才有效。如何启用文件夹选择器来合并所选文件夹中的文件。

【问题讨论】:

  • 欢迎来到 Stack Overflow! 当在其他地方找不到问题的解决方案时,此站点供程序员提问/回答问题。 (抱歉,不是“免费代码编写服务”)请参阅tour 和“How to Ask”,还有关于提供示例的重要提示“minimal reproducible example”。请表明您在寻求帮助之前努力自己找到了解决方案(针对特定问题)。你可以随时edit你的问题来展示你到目前为止所做的尝试。更多信息:help center 加上these great tips
  • 我错过了更新代码。现在我已经编辑了代码。请检查
  • 你能帮我找到一个解决方案来整合所选文件夹中的文件吗?仅当工作簿保存在我们需要合并的文件夹中时,上述代码才有效。请支持!
  • 您是否阅读了我在上面与您分享的链接?
  • 我刚刚尝试了你的代码,老实说,我很惊讶它运行得和它一样好。我以前从来没有理由将工作簿合二为一,但如果我这样做,谷歌将是我最好的朋友(一如既往)......这部分是因为你是唯一真正了解你的人'正在尝试做...我认为最大的问题是您没有正确解释自己,再加上 无法查看您的数据,其他人很难帮助您。另外,这里有很多人从某个地方复制代码,然后说“好的,伙计们,让它工作!”

标签: vba file consolidation


【解决方案1】:

考虑这个选项。有四个基本示例,此页面上的 3 个和示例工作簿中的 4 个: 1)合并文件夹中所有工作簿的范围(彼此下方) 2)从您选择的每个工作簿中合并一个范围(彼此下方) 3)合并文件夹中所有工作簿的范围(彼此相邻) 4) 使用 AutoFilter 合并文件夹中所有工作簿的范围

Sub Basic_Example_1()
    Dim MyPath As String, FilesInPath As String
    Dim MyFiles() As String
    Dim SourceRcount As Long, Fnum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim rnum As Long, CalcMode As Long

'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"

'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
    MyPath = MyPath & "\"
End If

'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
    MsgBox "No files found"
    Exit Sub
End If

'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
    Fnum = Fnum + 1
    ReDim Preserve MyFiles(1 To Fnum)
    MyFiles(Fnum) = FilesInPath
    FilesInPath = Dir()
Loop

'Change ScreenUpdating, Calculation and EnableEvents
With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
End With

'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
rnum = 1

'Loop through all files in the array(myFiles)
If Fnum > 0 Then
    For Fnum = LBound(MyFiles) To UBound(MyFiles)
        Set mybook = Nothing
        On Error Resume Next
        Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
        On Error GoTo 0

        If Not mybook Is Nothing Then

            On Error Resume Next

            With mybook.Worksheets(1)
                Set sourceRange = .Range("A1:C1")
            End With

            If Err.Number > 0 Then
                Err.Clear
                Set sourceRange = Nothing
            Else
                'if SourceRange use all columns then skip this file
                If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                    Set sourceRange = Nothing
                End If
            End If
            On Error GoTo 0

            If Not sourceRange Is Nothing Then

                SourceRcount = sourceRange.Rows.Count

                If rnum + SourceRcount >= BaseWks.Rows.Count Then
                    MsgBox "Sorry there are not enough rows in the sheet"
                    BaseWks.Columns.AutoFit
                    mybook.Close savechanges:=False
                    GoTo ExitTheSub
                Else

                    'Copy the file name in column A
                    With sourceRange
                        BaseWks.cells(rnum, "A"). _
                                Resize(.Rows.Count).Value = MyFiles(Fnum)
                    End With

                    'Set the destrange
                    Set destrange = BaseWks.Range("B" & rnum)

                    'we copy the values from the sourceRange to the destrange
                    With sourceRange
                        Set destrange = destrange. _
                                        Resize(.Rows.Count, .Columns.Count)
                    End With
                    destrange.Value = sourceRange.Value

                    rnum = rnum + SourceRcount
                End If
            End If
            mybook.Close savechanges:=False
        End If

    Next Fnum
    BaseWks.Columns.AutoFit
End If

退出TheSub: '恢复 ScreenUpdating、Calculation 和 EnableEvents 有应用程序 .ScreenUpdating = True .EnableEvents = 真 .Calculation = CalcMode 结束于 结束子

https://www.rondebruin.nl/win/s3/win008.htm

作为替代方案,我强烈推荐此插件。

https://www.rondebruin.nl/win/addins/rdbmerge.htm

【讨论】:

    猜你喜欢
    • 2022-12-14
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2021-01-30
    相关资源
    最近更新 更多