+ Reply to Thread
Results 1 to 9 of 9

***PLease help. Need to Merge directory of files into one file.***

  1. #1
    Shani
    Guest

    ***PLease help. Need to Merge directory of files into one file.***

    I have a bunch of excel files stored in c:/temp. I want to merge all of
    these files into one excel spreadsheet.


  2. #2
    Tom Ogilvy
    Guest

    RE: ***PLease help. Need to Merge directory of files into one file.***

    Sub consolidatebooks()
    Dim sh as Worksheet
    Dim sPath as String, sName as String
    Dim bk as workbook
    set sh = Activesheet
    sh.cells.clearcontents
    sPath = "C:\Myfolder\"
    sName = dir(sPath & "*.xls")
    do while sName <> ""
    set bk = workbooks.Open(sPath & sname)
    ' copy sheets?
    'bk.worksheets.copy After:= _
    thisworkbook.Worksheets(thisworbook.worksheets.count)
    ' copy some data
    ' bk.worksheets(1).Range("A1").currentRegion.copy _
    sh.cells(rows.count,1).End(xlup)(2)
    bk.close SaveChanges:=False
    sname = dir()
    Loop
    End Sub

    --
    Regards,
    Tom Ogilvy

    "Shani" wrote:

    > I have a bunch of excel files stored in c:/temp. I want to merge all of
    > these files into one excel spreadsheet.
    >
    >


  3. #3

    Re: ***PLease help. Need to Merge directory of files into one file.***

    This code gets all matching files from a directory - change my message
    box to whatever you need it to do (insert file by the sound of it!)


    Sub lookin()



    Set fs = Application.FileSearch
    Dim newbook, curbook, MyCount, myrange
    With fs
    .lookin = "c:\temp\usb"
    'LookIn should be changed to the location you want to use
    ..SearchSubFolders = True 'assuming you DO want to search subfolders

    .Filename = "*.xls"
    'if you are looking for other than DOC files, alter the line above
    If .Execute(SortBy:=msoSortByFileName,
    SortOrder:=msoSortOrderAscending) > 0 Then
    For i = 1 To .FoundFiles.Count
    MsgBox .FoundFiles(i)

    Next i
    Else
    MsgBox "There were no files found."
    End If
    End With
    End Sub


  4. #4
    Shani
    Guest

    Re: ***PLease help. Need to Merge directory of files into one file.***

    doesn't do anything


  5. #5
    Shani
    Guest

    Re: ***PLease help. Need to Merge directory of files into one file.***

    this doesnt do anything


  6. #6
    Shani
    Guest

    Re: ***PLease help. Need to Merge directory of files into one file.***

    there is a syntax error.


    If .Execute(SortBy:=msoSortByFileName


  7. #7
    Dave Peterson
    Guest

    Re: ***PLease help. Need to Merge directory of files into one file.***

    Tom left the definition of Merge to you.

    do while sName <> ""
    set bk = workbooks.Open(sPath & sname)
    ' copy sheets?
    'bk.worksheets.copy After:= _
    thisworkbook.Worksheets(thisworbook.worksheets.count)
    ' copy some data
    ' bk.worksheets(1).Range("A1").currentRegion.copy _
    sh.cells(rows.count,1).End(xlup)(2)
    bk.close SaveChanges:=False
    sname = dir()
    Loop

    The top part of this portion of the code copies all the worksheets in each
    workbook to this workbook.

    The bottom part of this portion of the code copies all the data in each sheet to
    a single sheet within this new workbook.

    Since you didn't give any hint about what you meant by merge, Tom left it up to
    you to decide.

    You'll have to uncomment the lines that do the type of work you want.

    ' copy sheets?
    ' copy some data

    Are instructions to you--don't uncomment them.

    And since you didn't give any rules, Tom's code uses Column A to determine the
    next available cell and assumes that all your data in each sheet is contiguous.

    ======
    Ps.

    I think you meant:

    Tom, Thank you for your response, but this doesn't seem to do anything when I
    try it. What can I do to make it work the way I want?

    And then you should give a little more info about how you want it to work.






    Shani wrote:
    >
    > this doesnt do anything


    --

    Dave Peterson

  8. #8
    Tom Ogilvy
    Guest

    Re: ***PLease help. Need to Merge directory of files into one file

    It doesn't open and close the workbooks in the directory?

    I gave you some choices, possibly you are frozen by indecision.

    Work with Aiden.

    --
    Regards,
    Tom Ogilvy


    "Shani" wrote:

    > this doesnt do anything
    >
    >


  9. #9
    Tom Ogilvy
    Guest

    Re: ***PLease help. Need to Merge directory of files into one file

    > there is a syntax error.
    >
    >
    > If .Execute(SortBy:=msoSortByFileName


    given this post, how could you post your first post to Aiden.

    Perhaps this isn't a serious request and you are just toying with use.
    Enjoy. <g>

    by the way, you just have copied the code improperly and haven't corrected
    for wordwrap.

    --
    Regards,
    Tom Ogilvy


    "Shani" wrote:

    > there is a syntax error.
    >
    >
    > If .Execute(SortBy:=msoSortByFileName
    >
    >


+ 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