|
-
Jun 2nd, 2006, 04:07 AM
#1
Thread Starter
Frenzied Member
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?
-
Jun 2nd, 2006, 04:55 AM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 2nd, 2006, 05:43 AM
#3
Thread Starter
Frenzied Member
Re: Merging excel workbooks
But I want to retain the sheet's name in the destination workbook.
-
Jun 3rd, 2006, 09:21 PM
#4
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|