Results 1 to 9 of 9

Thread: merging excel worksheets

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    merging excel worksheets

    Hi
    I have over 20 excel Worksheets in different workbooks that have the same headers.I want to merge them into one single worksheet in a specific workbook.
    How can I do it?
    Thanks in advance

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: merging excel worksheets

    carefully.
    Possibly with code.
    Or import into an mdb, and put them into the same table?

    If coding, you need to list/select all files you want to merge, then the code would copy and paste the range of data from each sheet into the combining sheet. Must be less than 65500 rows tho.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

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

    Re: merging excel worksheets

    Quote Originally Posted by engnouna
    I have over 20 excel Worksheets in different workbooks that have the same headers.I want to merge them into one single worksheet in a specific workbook.
    Is that 20 Sheets in 20 Books? ... or are there some Books with more than one Sheet? Are all of the books open when you start the macro, or do you need to open them? Might there be any other books open at the same time?
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: merging excel worksheets

    Is that 20 Sheets in 20 Books? ... or are there some Books with more than one Sheet? Are all of the books open when you start the macro, or do you need to open them? Might there be any other books open at the same time?
    There are 20 sheets in 20 books,every book contain one sheet,I want to merge them into one book containing one sheet which is the sum of the 20 sheets,there is no other book open at the same time

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

    Re: merging excel worksheets

    To do it from VB 6...
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Excel xx.0 Object library
    3. Private Sub Command1_Click()
    4.     Dim oApp As Excel.Application
    5.     Dim oWBMain As Excel.Workbook
    6.     Dim oWB As Excel.Workbook
    7.     Dim oSht As Excel.Worksheet
    8.     Dim i As Integer
    9.     Set oApp = New Excel.Application
    10.     'Create a new blank workbook to hold all the sheets
    11.     Set oWBMain = oApp.workbboks.Add
    12.     'Open Book1.xls - Book20.xls
    13.     For i = 1 To 20
    14.         'Open a workbook
    15.         Set oWB = oApp.Workbooks.Open("C:\Book" & i & ".xls")
    16.         'Copy and paste the sheet in our main workbook at the end of the sheet tabs
    17.         oWB.Sheets(1).Copy After:=oWBMain.Sheets(oWBMain.Sheets.Count)
    18.         'Close and clean up
    19.         oWB.Close SaveChanges:=False
    20.         Set oWB = Nothing
    21.     Next
    22.    
    23. End Sub
    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

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

    Re: merging excel worksheets

    There are 20 sheets in 20 books,every book contain one sheet,I want to merge them into one book containing one sheet
    Are the WorkBOOK names predictable ... like book01, book02, etc. ... or do they have independent names? If they have predictable names you can construct the file name in a loop. Otherwise, you'll have to create an array with all of the files names. I wouldn't guess that you would want all 20 of the files open at the same time. It is too easy to open them one at a time, pull the info off of the sheet for merging, and close the file. Answer this one and I'm sure you'll get some code on the next iteration.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2006
    Posts
    977

    Re: merging excel worksheets

    Hi webtest
    The names of the books are differents like indexDaily,indexmorning,mondaymorning...
    I just want to merge those files,so I don't care about opening them at once or no,
    Thanks in advance

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

    Re: merging excel worksheets

    If you place all the workbooks in a particular folder by themselves then you can enumerate the files in VBA and open them in a loop and use with my code.
    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

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

    Re: merging excel worksheets

    engnouna ...

    I thought I sent a message yesterday, but I guess something got fouled up. What I wanted to say was that you need to give us a good definition of the folder(s) where your 20 source files and your target file are found. Without that, it is nearly impossible to write the code to find and open the source sheets.

    Here is a function to append any Source sheet (excluding header rows, if any) to any Destination sheet, and a simple TEST subroutine. Make sure that you look at the "ActualUsedRange" function posted in this forum:Function "Actual Used Range" Posting
    Code:
    Sub TEST_APPEND()    'TEST the Append Call
        Dim rowLast As Long        'Last Row on the DESTINATION Sheet
        Dim NumHeaderRows As Long  'Number of Header Rows on ALL Source Sheets
        Dim arange As Range        'Miscellaneous Range Variable
        Dim aBool As Boolean       'Return Parameter for the Append call
        
        'Initialize the Number of Header Rows that are on ALL of the Source Sheets
        NumHeaderRows = 2
        'Find the Last Row on the Destination Sheet ... Source sheets will be appended AFTER this row.
        rowLast = ActualUsedRange(Sheets("sheet1")).Rows.Count
        'Do the APPEND Call
        aBool = AppendSrcToDest(Sheets("sheet3"), Sheets("Sheet1"), rowLast, NumHeaderRows)
        'Show the Results of the APPEND Call
        If aBool Then
            MsgBox "The APPEND completed successfully. Now Last Row = " & rowLast
        Else
            MsgBox "ERROR:  The APPEND FAILED!"
        End If
        
    End Sub
    '
    '=================================================================================================
    'Function to Append entire Source sheet (less Header Rows) to Destination sheet
    'By:  Arthur Du Rea    060603.0912
    '
    'Parameters;
    '  shtSrc   Sheet to copy to destination sheet
    '  shtdst   Sheet on which to append copy of Source Sheet
    '  rowDst   Number (Long) of the current Last Used Row in the Destination Sheet
    '  numHead  Number of Header Rows on the Source sheet that will NOT be copied
    'Returns:   TRUE on success;  FALSE on failure
    '  Updates Caller "Destination Row" to new "Last Row in Destination" due to added rows
    '
    'As written requires function "ActualUsedRange(aSheet)".  "UsedRange" might work equally well.
    '
    Function AppendSrcToDest _
           (ByRef shtSrc As Worksheet, ByRef shtDst As Worksheet, ByRef rowDst As Long, numHead As Long) _
            As Boolean
        Dim aRange As Range  'Miscellaneous Range Variable
        Dim aCell As Range   'Range of Last Cell in Source Sheet Actual Used Range
        
        'Set up the Error Return in case of errors
        On Error GoTo ERROR_RETURN
        'Find the range used in the Source Sheet
        Set aRange = ActualUsedRange(shtSrc)
        'If the Source Sheet is BLANK, don't do anything.  NOTE: UsedRange returns "A1" for Empty Sheet!
        If aRange Is Nothing Then AppendSrcToDest = True: Exit Function
        'If the Source Sheet only contains HEADER Rows don't do anything
        If aRange.Rows.Count <= numHead Then AppendSrcToDest = True: Exit Function
        'Find the Last Cell on the Source Sheet
        Set aCell = aRange.Cells.SpecialCells(xlCellTypeLastCell)
        'Set the Range on the Source Sheet to append to exclude the header rows
        Set aRange = shtSrc.Range(shtSrc.Cells(numHead + 1, "A"), aCell)
        'Append the actual Data Area of the Source Sheet to the Destination Sheet
        aRange.Copy Destination:=shtDst.Cells(rowDst + 1, "A")
        'Set the Return status to "PASSED"
        AppendSrcToDest = True
        'Update the Caller's Row Counter
        rowDst = rowDst + arange.Rows.Count
        'Clean House on Exit
        GoTo ALL_RETURNS
    
    ERROR_RETURN:
        'Set the return parameter to "FAILED"
        AppendSrcToDest = False
    ALL_RETURNS:
        'Clean House
        Set arange = Nothing
        Set aCell = Nothing
        On Error GoTo 0
        
    End Function
    If you tell us how and where to find your source and destination sheets, we will tell you how to iterate through them and use the above function to append your 20 source sheets to your target/destination sheet. In the mean time, you can modify the TEST_APPEND subroutine to test your own files/sheets.

    Good Luck and Good Programming!
    Last edited by Webtest; Jun 3rd, 2006 at 10:52 AM.
    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