【问题标题】:How do I merge multiple selected excel files in VBA?如何在 VBA 中合并多个选定的 Excel 文件?
【发布时间】:2016-01-20 03:12:39
【问题描述】:

我是 VBA 新手,我想知道如何使用 VBA 合并多个选定的 Excel 文件。我尝试对选择文件的部分进行编码。我已经研究并尝试在互联网上复制代码并进行了一些编辑。我了解到您可以添加过滤器,所以我这样做了。但有时,即使我添加了正确的过滤器(基于我的研究),excel 文件也不会显示。我真的需要合并多个选定的 excel 文件。我希望你能帮助我。

顺便说一句,我正在使用用户表单。一个按钮,可以选择和合并选定的文件。如果可能的话,我希望用户看到所选文件的路径。我还不知道如何做到这一点,或者我应该使用什么工具,比如列表框或什么。提前致谢!

更新!

我有一个选择多个 Excel 文件的代码。我现在需要的是如何合并我选择的文件。

Dim FileNames As Variant
  Dim Msg As String
  Dim I As Integer
  FileNames = Application.GetOpenFilename(MultiSelect:=True)
  If IsArray(FileNames) Then
      Msg = "You selected:" & vbNewLine
      For I = LBound(FileNames) To UBound(FileNames)
          Msg = Msg & FileNames(I) & vbNewLine
      Next I
      MsgBox Msg
      tbPath.Value = Msg
  Else
      MsgBox "No files were selected."
  End If

【问题讨论】:

  • 首先,你想如何合并文件???将所有工作表添加到新工作簿中???进入已经打开的工作簿???所有的床单都相等吗?如果工作表有相同的名称你想做什么???
  • 所有的 excel 文件只有一个工作表并且有不同的名称。我想为合并的文件创建一个新的工作簿/工作表。顺便说一句,我所说的 excel 文件是指工作簿。 :)

标签: vba excel


【解决方案1】:

这是我的代码...希望对您有所帮助。

    Sub mergeAllFiles()

    Dim This As Workbook 'Store the book with the macro
    Dim TmpB As Workbook 'store the book that has the sheets (one per book)
    Dim AllB As Workbook 'book to send all the books
    Dim sht As Worksheet 'the only sheet every book

    Dim FileNames As Variant
    Dim Msg As String
    Dim I As Integer

    Set This = ThisWorkbook
    FileNames = Application.GetOpenFilename(MultiSelect:=True)
    If IsArray(FileNames) Then
        Workbooks.Add 'add a new book to store all the sheets
        Set AllB = ActiveWorkbook
        AllB.SaveAs This.Path & "\allSheetsInOne" & SetTimeName & ".xlsx", 51
        'The function is to store a different name every time and avoid error
        Msg = "You selected:" & vbNewLine

        For I = LBound(FileNames) To UBound(FileNames)
            Workbooks.Open Filename:=FileNames(I)
            Set TmpB = ActiveWorkbook
            TmpB.Activate
            Set sht = ActiveSheet 'because you say that the book has only one sheet
            sht.Copy Before:=AllB.Sheets(Sheets.Count) 'send it to the end of the sheets
            TmpB.Close 'we don't need the book anymore
            Set TmpB = Nothing 'empty the var to use it again
            Set sht = Nothing
            Msg = Msg & FileNames(I) & vbNewLine
        Next I
        MsgBox Msg
        tbPath.Value = Msg
    Else
        MsgBox "No files were selected."
    End If
End Sub


Function SetTimeName()
Dim YY
Dim MM
Dim DD
Dim HH
Dim MI
Dim SS
Dim TT

YY = Year(Date)
MM = Month(Date)
DD = Day(Date)
HH = Hour(Now)
MI = Minute(Now)
SS = Second(Now)

TT = Format(YY, "0000") & Format(MM, "00") & Format(DD, "00") & Format(HH, "00") & Format(MI, "00") & Format(SS, "00")

SetTimeName = TT
End Function

告诉我是否需要改进。

【讨论】:

  • 感谢您的代码。但是,我从 sht.Copy Before:=AllB.Sheets(Sheets.Count) '将其发送到工作表末尾的行中得到一个错误它说下标超出范围。
  • 你导入了多少本书???你用的是哪个版本的excel???您是使用 F8(逐步)还是任何按钮(让宏运行)运行代码???
  • 目前,我有 5 个工作簿,但程序应该很灵活,可以合并不同数量的工作簿。我使用的是 Excel 2010。运行代码时,我只需按 F5。我正在使用用户表单。
【解决方案2】:

从这里使用我的代码:

Multi-Select Files and open

编辑代码以满足您的要求。

Sub OPenMultipleWorkbooks()
'Open Multiple .xlsx files
    Application.DisplayAlerts = False
    Dim wb As Workbook, bk As Workbook
    Dim sh As Worksheet
    Dim GetFile As Variant, Ws As Worksheet

    Set wb = ThisWorkbook
    Set sh = wb.ActiveSheet

    For Each Sheet In Sheets
        If Sheet.Name <> sh.Name Then Sheet.Delete
    Next Sheet

    ChDrive "C:"
    Application.ScreenUpdating = False

    GetFile = Application.GetOpenFilename(FileFilter:="XLSX(*.xlsx), *.xlsx", Title:="Open XLSX- File", MultiSelect:=True)

    On Error Resume Next

    If GetFile <> False Then

        On Error GoTo 0

        For i = 1 To UBound(GetFile)

            Set bk = Workbooks.Open(GetFile(i))
            Sheets(1).Move Before:=wb.Sheets(1)
            bk.Close True
        Next i

    End If

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多