+ Reply to Thread
Results 1 to 14 of 14

EXCEL VBA challenge . how to merge workbooks into one

Hybrid View

  1. #1
    Registered User
    Join Date
    01-30-2015
    Location
    Europe
    MS-Off Ver
    2010
    Posts
    31

    EXCEL VBA challenge . how to merge workbooks into one

    Gurus,

    i have a excel challenge and i tried asking some gurus here at my work, and No one was able to solve this.

    i need a vba code to put in the workbook named MASTER

    master workbook is located in C:/Myfiles

    now there are multiple workbooks in the same directory and all of these workbooks have same numbers of identical sheets that has same names, same field structure; same column headings, and the same column order .

    now the VBA code should combine all of these workbooks sheets into its related sheet into one workbook

    for example lets say that there are 12 workbooks having 5 sheets sheet1 sheet2 sheet3 sheet4 and supposedly each of these worksheets has 10 rows of data excluding the header row.

    when i merge them with VBA then my merged single workbook file will have sheet1 sheet2 sheet3 sheet4 each of these sheets in the merged file idealy should have 120 rows excluding the header row, becuase we had 12 workbooks.

    the only addition would be that i need one new column added to the end of columns on each of the sheets in merged file that shows the original workbook name for those rows.

    any help is appreciated

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: EXCEL VBA challenge . how to merge workbooks into one

    I'm logging off now but here's some code that you could edit for your task:

    Sub PasteBase(): Dim wb As Workbook, ws As Worksheet, wd As Worksheet, r As Long
    Dim s As String, p As String, U As String: p = ActiveWorkbook.path & "\": U = Dir(p)
    
    'Unless you know the Path for these files you'll need to have one of them up at invocation
    
    For Each wb In Workbooks                 'If "Database" is open then Set receiving sheet
    If wb.Name Like "Database.xls*" Then GoTo SetDB
    Next: Workbooks.Open FileName:=p & "Database"  'Else open "Database"
    SetDB: Set wd = Workbooks("Database").Sheets("Direct Personel"): r = 3
    SetaBook:
    If U Like "Database.xl*" Then GoTo GetaBook
    If InStr(1, U, ".xl") = 0 Then GoTo GetaBook
    If InStr(1, U, "Report") Then
    Workbooks.Open FileName:=p & U, UpdateLinks:=0
    Set wb = ActiveWorkbook: Set ws = wb.Sheets("Direct Personel")
    ws.Range("A3:D59").Copy wd.Cells(r, 1): wb.Close SaveChanges:=False
    r = r + 59: End If
    GetaBook: U = Dir()
    If U = "" Then Exit Sub
    GoTo SetaBook: End Sub
    in the meantime you could supply some more information - but it's best if you:

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    01-30-2015
    Location
    Europe
    MS-Off Ver
    2010
    Posts
    31

    Re: EXCEL VBA challenge . how to merge workbooks into one

    MASTER MERGED.xlsbCOMPANYB.xlsbCOMPANYA.xlsb


    thanks for your help.

    here is the attachments. the companyA and companyB files are the workbooks to be merged and MASTER MERGED is the example on how the end result should look like after the merger with VBA is done.

    grateful for your help on this.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: EXCEL VBA challenge . how to merge workbooks into one

    I'll see what I can do tomorrow!

    And, Thanks for the rep!

  5. #5
    Registered User
    Join Date
    01-30-2015
    Location
    Europe
    MS-Off Ver
    2010
    Posts
    31

    Re: EXCEL VBA challenge . how to merge workbooks into one

    xladept

    thank you very much.

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: EXCEL VBA challenge . how to merge workbooks into one

    You're welcome!

    Try this:

    Sub PasteBase(): Dim wb As Workbook, ss As Worksheet, wm As Workbook, sm As Worksheet
    Dim r As Long, c As Long, m As Long, P As String, U As String
    P = "C:\MyFiles\": U = Dir(P):Set wm = Workbooks("MASTER MERGED")
    SetaBook:
    If U Like "MASTER*" Then GoTo GetaBook
    If InStr(1, U, ".xl") = 0 Then GoTo GetaBook
    If InStr(1, U, "COMPANY") Then
    Workbooks.Open Filename:=P & U, UpdateLinks:=0: Set wb = ActiveWorkbook
                            For Each ss In wb.Worksheets
                            For Each sm In wm.Worksheets
                            If ss.Name = sm.Name Then GoTo GetLimits
                            Next sm
         wm.Worksheets.Add(After:=wm.Worksheets(wm.Worksheets.Count)).Name = ss.Name
                            
                            
    GetLimits:  Set sm = wm.Sheets(ss.Name)
                r = ss.Range("A:A").Find("*", , , , xlByRows, xlPrevious).Row
                c = ss.Rows(1).Find("*", , , , xlByColumns, xlPrevious).Column
                If sm.Range("A1") = "" Then m = 2 Else _
                m = sm.Range("A:A").Find("*", , , , xlByRows, xlPrevious).Row + 1
                If m < 3 Then ss.Rows(1).Copy sm.Cells(1, 1)
                ss.UsedRange.Offset(1).Copy sm.Cells(m, 1)
                sm.Cells(m, c + 1).Resize(r - 1, 1) = wb.Name
    Next ss: End If: wb.Close SaveChanges:=False
    
    GetaBook: U = Dir()
    If U = "" Then Exit Sub
    
    
    GoTo SetaBook: End Sub
    Last edited by xladept; 09-25-2015 at 01:21 AM.

  7. #7
    Registered User
    Join Date
    01-30-2015
    Location
    Europe
    MS-Off Ver
    2010
    Posts
    31

    Re: EXCEL VBA challenge . how to merge workbooks into one

    i got error of Run-time error 9
    subscript out of range in this line
    : Set wm = Workbooks("MASTER MERGED")
    do i run this code from Master Merged file or from another workbook?

    also what are these colons : you used in your code? i have not seen this sort of VBA programming before.

    i would be grateful if you could get this error fixed.

    thanks.

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: EXCEL VBA challenge . how to merge workbooks into one

    Hi,

    The sample file you sent was named "MASTER MERGED" - you must have that file with that name open since that's where you wanted the code to be.

    The colons allow me to put more than one line of code on a line i.e.

    P = "C:\MyFiles\": U = Dir(P): Set wm = Workbooks("MASTER MERGED")
    would, otherwise, need to be written as:

    P = "C:\MyFiles\"
     U = Dir(P)
    Set wm = Workbooks("MASTER MERGED")
    Here's your file - try running it from this
    Attached Files Attached Files
    Last edited by xladept; 09-25-2015 at 02:55 PM.

  9. #9
    Registered User
    Join Date
    01-30-2015
    Location
    Europe
    MS-Off Ver
    2010
    Posts
    31

    Re: EXCEL VBA challenge . how to merge workbooks into one

    xladept

    thanks very much. i changed the Workbooks("MASTER MERGED") to Workbooks("MASTER MERGED.xlsb") adding the file extension then it worked.

    thanks for clarifying the colon

    i have a follow up question, some people use the _ to move to the next line in VBA.
    then my question is which is most preferable and better to use the underscore or the colon as you used?

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: EXCEL VBA challenge . how to merge workbooks into one

    The colon and the underscore are used differently:

    The underscore is a line continuation while the colon makes a new line in the code

    Glad you got it working - I didn't need the extension on my system

  11. #11
    Registered User
    Join Date
    08-19-2006
    Location
    New York, NY
    MS-Off Ver
    MS Office 2013
    Posts
    33

    Re: EXCEL VBA challenge . how to merge workbooks into one

    Hi,

    I am trying to follow your example, but I am getting an error when I step through the code at
    GetaBook: U = Dir()
    The error code is:

    "Run-time error '5:

    Invalid procedure call or argument."

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: EXCEL VBA challenge . how to merge workbooks into one

    Hi Top,

    Did you have:
    U = Dir(P)
    somewhere before the
    U = Dir()
    ?

  13. #13
    Registered User
    Join Date
    08-19-2006
    Location
    New York, NY
    MS-Off Ver
    MS Office 2013
    Posts
    33

    Re: EXCEL VBA challenge . how to merge workbooks into one

    Yes, I have U= DIR(P) in line 3

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: EXCEL VBA challenge . how to merge workbooks into one

    Hi Top,

    Start another thread and give a link to it here then:

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Excel 2007 : Merge excel workbooks
    By Dutchvillain in forum Excel General
    Replies: 1
    Last Post: 10-25-2010, 03:43 PM
  2. Excel Mail Merge Challenge
    By mimottershaw in forum Excel General
    Replies: 2
    Last Post: 02-24-2008, 11:34 AM
  3. Compare and Merge Workbooks in Excel
    By colin41 in forum Excel General
    Replies: 7
    Last Post: 03-26-2007, 07:21 PM
  4. [SOLVED] How do I merge two different excel workbooks
    By Jesse R. in forum Excel General
    Replies: 2
    Last Post: 08-22-2006, 10:05 AM
  5. [SOLVED] How to merge various workbooks into 1 worksheet in Excel
    By Sharm in forum Excel General
    Replies: 1
    Last Post: 08-22-2005, 04:05 AM
  6. [SOLVED] Is it possible to merge workbooks in excel?
    By trm5214 in forum Excel General
    Replies: 3
    Last Post: 05-03-2005, 05:06 PM
  7. merge excel workbooks
    By Theresa in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-06-2005, 03:06 PM

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