+ Reply to Thread
Results 1 to 5 of 5

Open Folder Dialog Box with Combine Files Code

Hybrid 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

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Open Folder Dialog Box with Combine Files Code

    Something like this maybe:

    ' This sets the path of the folder where the TSR workbooks to be imported are stored.
        OldDir = CurDir
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "C:\"
            .Show
            If .SelectedItems.Count > 0 Then
                fPath = .SelectedItems(1)
            Else
                MsgBox "Folder selection cancelled"
                Exit Sub
            End If
        End With
        ChDir fPath
        fName = Dir("*.xl*")

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Open Folder Dialog Box with Combine Files Code

    With events, alerts etc being turned off at the beginning of your code it's also worth building in some error handling to turn them back on should things bomb out for any reason:

    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
        
        On Error GoTo Error_Handler
        
    ' 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
        With Application.FileDialog(msoFileDialogFolderPicker)
            .InitialFileName = "C:\"
            .Show
            If .SelectedItems.Count > 0 Then
                fPath = .SelectedItems(1)
            Else
                MsgBox "Folder selection cancelled"
                GoTo Clean_Exit
            End If
        End With
        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 restores the original working path.
        ChDir OldDir
    
    Clean_Exit:
    
    ' This resets the settings we changed to speed up the macro.
        Application.ScreenUpdating = True
        Application.EnableEvents = True
        Application.DisplayAlerts = True
        Application.Calculation = xlAutomatic
        Exit Sub
    
    Error_Handler:
    
        MsgBox Err & " - " & Err.Description
        GoTo Clean_Exit
    
    End Sub

    Dom

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

    Re: Open Folder Dialog Box with Combine Files Code

    Oh man, perfect!!! thank you so much!

  5. #5
    Registered User
    Join Date
    06-15-2015
    Location
    spokane washington
    MS-Off Ver
    2011
    Posts
    4

    Re: Open Folder Dialog Box with Combine Files Code

    This gave me an error without showing a prompt to select the folder where the excel files are housed. any suggestions?

+ Reply to Thread

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