Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 8th, 2007, 05:43 AM
Registered User
 
Join Date: Nov 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Merge many excel files into one excel files

1. I have a master file (master.xls), currently empty
2. I have 80 excels files (all look a like but their worksheet name are different)
3. I would like all the data from these worksheet to be appended to master.xls with a click of button.
4. I tried using some codes from this forum but they did not work.
5. All the source file starts with ums bla bla.xls
6. please if possible send me macro code to perform this operation.

Please suggest.

 
Old November 9th, 2007, 11:36 AM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Do you want to append all files without first checking for duplicates? If you need to check for duplicates you'll need to cycle through each source sheet 1 row at a time and will need to modify the code below for that.

Put all source files in the same folder. In this case, in C:\SourceXLS:
-------------------------------------------------------------
Private Sub PullFiles_Button_Click()

'Processes worksheets in sDir copying them to FullList Tab
  Dim oTarget As Worksheet, oSource As Worksheet, sOpenWB As String, sThisWB As String
  Dim sDir As String, iStartRow As Long, bNoError As Boolean
  sThisWB = ActiveWorkBook.FullName
  sThisWB = Right(sThisWB, Len(sThisWB) - Len(ActiveWorkbook.Path) - 1)
  sDir = "C:\SourceXLS"
  Set oTarget = Workbooks(sThisWB).Worksheets("FullList")
  sOpenWB = Dir(sDir & "\*.xls") 'Gets First file from directory of all *.xls files in sDir folder
  If sOpenWB = sThisWB Then sOpenWB = Dir 'Gets Next File if current is this master workbook
  if sOpenWB = "" Then
    MsgBox "No XLS files found in folder or bad path",,No Data Files Found"
    Exit Sub
  End If
  iStartRow = 2 'Pastes starting at row 2 assuming that titles are row 1
  Do While sOpenWB <> ""
    bNoError = OpenSource(sDir & "\" & sOpenWB)
    If Not bNoError Then
      MsgBox "Error Opening Workbook " & sDir & "\" & sOpenWB & ", check path / Filename and try again", _
        "File Open Error"
      Exit Sub 'Halts processing on first failure to read file that should exist
    End If
    Set oSource = Workbooks(sOpenWB).ActiveSheet 'Assumes active sheet on opening workbook is only 1 to copy
    oSource.Range("A2:G2").Select 'Assumes Data is in columns A to G and data starts in row 2 no empty rows
    oSource.Selection.End(xlDown).Select 'Selects all records going down until empty row
    oSource.Selection.Copy Destination:=oTarget.Cells(iStartRow, 1)
    iStartRow = oTarget.Range("A2").End(xlDown).Row
    Workbooks(sOpenWB).Close SaveChanges:=False
    sOpenWB = Dir 'Gets Next File
    If sOpenWB = sThisWB Then sOpenWB = Dir 'Gets Next File if current is this master workbook
  Loop

End Sub

Private Function OpenSource(sToOpen As String) As Boolean

'Tries to open file and returns false if fails
  On Error GoTo FailedOpenSource
    Workbooks.Open FileName:=sToOpen
  On Error GoTo 0
  OpenSource = True
FailedOpenSource:

End Function
-------------------------------------------------------------

This is pretty simplistic but it should point you in the right direction.

Hope this helped.


 
Old November 14th, 2007, 06:23 PM
Registered User
 
Join Date: Jan 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to milindsaraswala Send a message via Yahoo to milindsaraswala
Default

I need same solution but their is slightly change what i need it should update row value. I mean in source excel sheet it will some data in column which will not have in destination file. I want merge that as well as add new rows which is not available in sheet. And their will be 1 column in destination file which will work as unique value.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating Excel FIles from resource Files abinashpatra ASP.NET 1.0 and 1.1 Basics 0 July 28th, 2008 02:07 AM
export excel files collie ASP.NET 1.x and 2.0 Application Design 2 January 3rd, 2008 09:09 AM
Comparing two different excel files anup.bihani Excel VBA 3 December 4th, 2006 06:00 AM
Excel files Ghost ajindal General .NET 0 January 19th, 2005 08:40 AM
Opening Excel and Excel files dinosaur_uk VB.NET 2002/2003 Basics 3 September 17th, 2004 03:22 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.