Results 1 to 4 of 4

Thread: Merging excel workbooks

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Posts
    1,384

    Merging excel workbooks

    I have a number of workbooks. Some may only contain 1 worksheet, some more. I want to programmatically merge all worksheets into the one, new, workbook.

    I can sucessfully do this, when I know how many worksheets in each workbook and each worksheet name. However, I would prefer not to hardcode in the worksheet names. Can I write some code that wil figure out how many worksheets are contained in each workbook, and the worksheet name. Then loop through to merge all worksheets into the one workbooks?
    Mel

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Merging excel workbooks

    Use the Sheets collection to get the number of sheets in the workbook. Then to refer to the workbook or sheet without hard coding the names, use their index numbers instead.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Posts
    1,384

    Re: Merging excel workbooks

    But I want to retain the sheet's name in the destination workbook.
    Mel

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: Merging excel workbooks

    This will copy all of the sheets, including all formatting, headers, page setup, etc. from a source workbook into a destination workbook:
    Code:
        'This will append all sheets from the "bBook" after the last sheet of the "aBook"
        Call Macro1(bBook, aBook)
    
    Sub Macro1(ByRef wbkSrc As Workbook, ByRef wbkDst As Workbook)
        Dim aSheet As Worksheet
        
        For Each aSheet In wbkSrc.Sheets
            aSheet.Copy after:=wbkDst.Sheets(wbkDst.Sheets.Count)
        Next aSheet
        
    End Sub
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width