+ Reply to Thread
Results 1 to 7 of 7

combine several spreadsheets into one

Hybrid View

david2005wang combine several spreadsheets... 06-24-2009, 03:15 PM
JBeaucaire Re: combine several... 06-24-2009, 05:26 PM
david2005wang Re: combine several... 06-25-2009, 03:34 PM
JBeaucaire Re: combine several... 06-25-2009, 04:54 PM
david2005wang Re: combine several... 06-26-2009, 12:54 AM
JBeaucaire Re: combine several... 06-26-2009, 11:45 AM
david2005wang Re: combine several... 06-26-2009, 03:30 PM
  1. #1
    Registered User
    Join Date
    06-09-2009
    Location
    UAE
    MS-Off Ver
    Excel 2013
    Posts
    20

    Smile combine several spreadsheets into one

    I am so frustrated to combine several spreadsheets into all in one, and seperate all in one spreadsheet into several with the same header frenquently. Pls check the attached example excel spreadsheet., could anyone teach me how to program in vba to achieve the result as above?

    Many thanks.
    Attached Files Attached Files
    Last edited by david2005wang; 06-26-2009 at 03:31 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: combine several spreadsheets into one

    Here are two simple array macros to consolidate all to one sheet, or parse all to one out to the vendor sheets.
    Option Explicit
    
    Sub Consolidate()
    Dim NR As Long, ws As Worksheet, wsA As Worksheet
    If MsgBox("Clear the All in one report and collate new info from Vendor sheets?", _
        vbYesNo + vbQuestion) = vbYes Then
        On Error GoTo ErrorHandler
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        
        Sheets("All in one").Activate
        Set wsA = ActiveSheet
        Range("A2", Range("A2").SpecialCells(xlCellTypeLastCell)).ClearContents
        NR = 2
        
        For Each ws In Sheets(Array("AEG", "ARP", "ARV", "BEA", "CPM", "EFX", "KER", "MIT", "NUO", "PTE", "SND", "TFS", "TTE"))
            ws.Activate
            Range("A3", Range("A3").SpecialCells(xlCellTypeLastCell)).Copy wsA.Range("A" & NR)
            NR = wsA.Range("A2").End(xlDown).Row + 1
        Next ws
    End If
    wsA.Activate
    Set wsA = Nothing
    ResetAll:
       Application.ScreenUpdating = True
       Application.EnableEvents = True
       Exit Sub
    
    ErrorHandler:
        MsgBox Err.Number & " - " & Erl & " - " & Err.Description
        Resume ResetAll
    End Sub
    
    Sub Parse()
    Dim LR As Long, LR2 As Long, i As Long, MyArray(), ws As Worksheet, wsA As Worksheet
    If MsgBox("Clear ALL the vendor sheets and parse out new info from the All In One sheet?", _
        vbYesNo + vbQuestion) = vbYes Then
        On Error GoTo ErrorHandler
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        
        Set wsA = Sheets("All in one")
        wsA.Activate
        MyArray = Array("AEG", "ARP", "ARV", "BEA", "CPM", "EFX", "KER", "MIT", "NUO", "PTE", "SND", "TFS", "TTE")
        Range("A1").AutoFilter
        LR = Range("A" & Rows.Count).End(xlUp).Row
        
        For i = 0 To UBound(MyArray)
            Set ws = Sheets(MyArray(i))
            LR2 = ws.Range("A" & Rows.Count).End(xlUp).Row
            ws.Range("A3:AA" & LR).ClearContents
            Range("A1").AutoFilter Field:=9, Criteria1:=MyArray(i)
            Range("A2:R" & LR).SpecialCells(xlCellTypeVisible).Copy ws.Range("A3")
        Next i
            
            Range("A1").AutoFilter
    End If
    
    Set wsA = Nothing
    
    ResetAll:
       Application.ScreenUpdating = True
       Application.EnableEvents = True
       Exit Sub
    
    ErrorHandler:
        MsgBox Err.Number & " - " & Erl & " - " & Err.Description
        Resume ResetAll
    End Sub
    How to use the macros:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Get out of VBA (Press Alt+Q)
    6. Save your sheet

    The macros are installed and ready to use. Press Alt-F8 and select them from the macro list.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-09-2009
    Location
    UAE
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: combine several spreadsheets into one

    Hi, JBeaucaire,

    Thanks for your programming.

    It should be worked I believe. However, when I ran this program either in Debug status or select the Macro in the M.S. spreadsheet, and the spreadsheet popup " 9-0 subscript out of range" and then the program stop.

    I trust you have tried in your computer about this program.

    by thy way, My M.S is 2003 version.

    Any clues will be highly appreciated.

    Regards

    Wang

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: combine several spreadsheets into one

    That error usually means the macro can't find a specific sheet. If you've tried this sheet on a different one that the one you uploaded, it is expecting to find the same sheets.

    Try in the attached version of your original file.

    The two macros are "Consolidate" and "Parse".
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    06-09-2009
    Location
    UAE
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: combine several spreadsheets into one

    Dear Mr. JBeaucaire, Thanks so munch for you prompt response, really.
    What you said is exactly right.
    I found the reason that your program don't work for my case.
    When I delete the sub-spreadsheets, such as "AEG " "ARP"ARVBEACPMEFXKERMITNUOPTESNDTFS"TTE",
    then I run the macro again and click "Parce", the error " 9-0 subscript out of range " popup again; Or, When I delete the "all in on "spreadsheet, and click " consolidate" with the same situiation.

    I think there much a way to automactically delete the original, if exist, sub-spreadsheets,and setup new sub-spreadsheets everytime, when I click "Parce", And, automactically delete the original, if exist, "All in one" spreadsheets,and setup new one.

    Thanks for you further assisatance.

    Regards

    P.S. could you leave you email address in order to "bother" you again in the future.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: combine several spreadsheets into one

    Please do not delete the sheets. As stated earlier, it is expected that those sheets will be there. The macros clear away the old data on those sheets, so you do not need to delete them.

    Consolidate "clears the data from the All in one sheet" without deleting the sheet itself. This way we don't have to recreate headers and stuff.

    Parse clears the individual sheets of data without deleting the sheets and then parses out the data from ALL IN ONE back out to all the sheets.

  7. #7
    Registered User
    Join Date
    06-09-2009
    Location
    UAE
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: combine several spreadsheets into one

    coreect. through what you said, it works.

    thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1