Results 1 to 5 of 5

Open Folder Dialog Box with Combine Files Code

Threaded View

  1. #1
    Registered User
    Join Date
    08-14-2007
    Posts
    27

    Open Folder Dialog Box with Combine Files Code

    I have the below code which combines workbooks into one sheet. The path to the folder containing the workbooks is specified in the code. However, what I want to do is use the folder open dialog box so that it prompts the user to select the folder where the files are. I've tried various ways of doing this but so far have not been able to figure it out. Can anyone assist? I am hoping to accomplish this without dramatically modifying my existing code. Thank you

    Sub Consolidate()
    ' This macro imports (combines) all TSR workbooks into one sheet.
    
    ' This defines various objects
        Dim fName As String, fPath As String, fPathDone As String, OldDir As String
        Dim wbkOld As Workbook, wbkNew As Workbook, ws As Worksheet
        Dim LR As Long, NR As Long
    
    ' This speeds up the macro.
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        Application.Calculation = xlCalculationManual
        
    ' This defines that the current workbook is the detination for all the TSR workbooks to be copied too.
        Set wbkNew = ThisWorkbook
        wbkNew.Activate
        Sheets("Master").Activate
        
    ' This defines the range of each TSR workbook to be copied.
        NR = Range("A8:BP27").End(xlUp).Row + 1
    
    ' This sets the path of the folder where the TSR workbooks to be imported are stored.
        OldDir = CurDir
        fPath = "C:\Documents and Settings\christopher.kline\Desktop\Work Files\Projects\Pending\Automated TSR Process\New TSR Template\TSR Combine Tests\Test A (5 Workbooks)"
        ChDir fPath
        fName = Dir("*.xl*")
        
    ' This imports the first sheet from each TSR workbook in the folder.
        Do While Len(fName) > 0
        Set wbkOld = Workbooks.Open(fName)
        Sheets(1).Activate
    ' This imports only rows where column BO is not blank.
        LR = Range("BO" & Rows.Count).End(xlUp).Row
    ' This copies only the given range of data from each TSR.
        Range("A8:BP27").Copy
    ' This pastesvalues in column A of the destination workbook.
        wbkNew.Sheets("Master").Range("A" & NR).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                    
    ' This closes the TSR workbooks and moves on to the next TSR workbook and the next empty row in the destination workbook.
        wbkOld.Close False
        NR = Range("A" & Rows.Count).End(xlUp).Row + 1
        fName = Dir
        Loop
    
    ' This resets the settings we changed to speed up the macro.
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Application.DisplayAlerts = True
        Application.Calculation = xlAutomatic
    
    ' This restores the original working path.
        ChDir OldDir
        
    End Sub
    Last edited by wealthistime; 04-14-2011 at 05:56 AM. Reason: SOLVED

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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