+ Reply to Thread
Results 1 to 5 of 5

Merging of excel files which has 10 sheets in each

Hybrid View

  1. #1
    Registered User
    Join Date
    07-23-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    9

    Merging of excel files which has 10 sheets in each

    Hi All,

    I am in a difficult position.
    I have two workbooks Workbook1 & Workbook2. Both the workbooks have the same sheet names.I have twenty sheets in each workbook.
    My problem lies here i want to match the sheet name of workbook1 with the sheetname of workbook2 if they are same then copy the data of woksheet of workbook2 into workbook1 as the data is different in both the workbooks or copy the data of into a new workbook.

    SAY EXAMPLE
    Workbook1 has sheets with NAMES Age, location,Hobby etc,,,,,,,,,,,,,
    Workbook2 has sheets with NAMES Age, location,Hobby etc,,,,,,,,,,,,,

    I want a new workboo3 Which will have the data of workbook1 Age sheet and data of Workbook2 Age sheet in new sheet of workbook3 similarly for the remaining twenty sheets.

    or it has to copy the data of worksheet2 Age sheet data in Workbook1 Age sheet.


    Attached are the sample files

    Please help me on this as early as possible.

    I have serached the web it gives the solution to merge only the first worksheet not the remaining sheets



    Regards,
    Salma
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    Re: Merging of excel files which has 10 sheets in each

    Hi
    It is not clear from your example how you want the data consolidated. book3 has same data as book2. Book1 shows Pivot table. Pl give us a better picture of what you would like to see in the merged file
    Ravi

  3. #3
    Registered User
    Join Date
    05-05-2009
    Location
    Portugal
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: Merging of excel files which has 10 sheets in each

    Sub ImportDistricts()
    'Instructional Message Box
    MsgBox "Click OK to acess explorer." & vbCrLf & _
    "Navigate to the folder path that contains" & vbCrLf & _
    "the District workbooks you want to import." & vbCrLf & vbCrLf & _
    "When you get inside that folder path," & vbCrLf & _
    "use your mouse to select one workbook," & vbCrLf & _
    "or use the Ctrl button with your mouse" & vbCrLf & _
    "to select as many District workbooks" & vbCrLf & _
    "as you want from that same folder path." & vbCrLf & vbCrLf & _
    "There is a limit of one path per macro run," & vbCrLf & _
    "but as many workbooks per path as you want." & vbCrLf & vbCrLf & _
    "Please click OK to get started.", 64, "Instructions..."
    'Variable declarations
    Dim Tlr As Long, Alr As Long, u As String, v As String, w As Worksheet, x As Integer, y As Integer, z As Variant
    z = Application.GetOpenFilename(FileFilter:="Excel files (*.xls), *.xls", MultiSelect:=True)
    'Prepare Excel
    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With
    'Open loop for action to be taken on all selected workbooks.
    On Error Resume Next
    For x = 1 To UBound(z)
    'Error handler within code if Cancel is clicked in Open dialog.
    If Err.Number = 13 Then
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    MsgBox "You did not select any workbooks." & vbCrLf & _
    "Click OK to exit this macro.", 48, "Import action cancelled."
    On Error GoTo 0
    Err.Clear
    Exit Sub
    End If
    'Open the workbook(s) that were selected.
    Workbooks.Open (z(x))
    'Open loop to act on every sheet.
    For Each w In ActiveWorkbook.Worksheets
    'Identify sheet name
    v = w.Name
    'Determine if the sheet name in the District workbook also exists in the Main workbook.
    'If not, create one in the Main workbook. If so, disregard and move on.
    Err.Clear
    On Error Resume Next
    u = ThisWorkbook.Worksheets(v).Name
    If Err.Number <> 0 Then
    With ThisWorkbook
    .Worksheets.Add(After:=.Sheets(.Sheets.Count)).Name = v
    End With
    End If
    On Error GoTo 0
    Err.Clear
    'At this point we know there is a sheet name in the Main workbook
    'for every sheet name in the District workbook, which will remain unique, not duplicated.
    'Determine the next available row in the Main workbook for this particular sheet in the District workbook.
    'If structures are to guard against run time error if sheet(s) is / are blank.
    If Application.CountA(w.Cells) <> 0 Then
    On Error Resume Next
    Alr = w.Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    On Error GoTo 0
    Else
    Alr = 1
    End If
    If Application.CountA(ThisWorkbook.Worksheets(v).Cells) <> 0 Then
    Tlr = ThisWorkbook.Worksheets(v).Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
    Else
    Tlr = 1
    End If
    'Copy the rows from the District sheet to the Main workbook's sheet whose name is the same.
    w.Rows("1:" & Alr).Copy ThisWorkbook.Worksheets(v).Cells(Tlr, 1)
    'Continue and terminate the loop for all worksheets in the District workbook.
    Next w
    'Close the District workbook without saving it.
    ActiveWorkbook.Close False
    'Continue and terminate the loop for the selected District workbooks.
    Next x
    'Restore Excel.
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With
    'Message box to inform user the job is complete.
    MsgBox "The import is complete.", 64, "Done !!"
    End Sub
    Try this...
    Last edited by jmaocubo; 07-24-2009 at 09:48 AM.

  4. #4
    Registered User
    Join Date
    07-23-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    9

    Wink Re: Merging of excel files which has 10 sheets in each

    Its working absolutely fine.Thanks a lot!!!!!!!!!!! Kudos to you!!!!!!!!!!!!!!!

    But I need a small help on the same i want to add some empty rows in each worksheet of main work book so that i can differentiate the data of book1 and book2. IS IT POSSIBLE?
    The second book data is from pivot table and i want to remove the structure of the pivot table data while copying them to main workbook i just want to paste the data as values..
    How to do that?

    Thanks,
    Salma

  5. #5
    Registered User
    Join Date
    07-23-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Merging of excel files which has 10 sheets in each

    Can you pls help me in modifiing the code jmaocubo.

+ 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