Getting a folder path to tie to a variable in my script

bobrandom123

New Member
Joined
Apr 19, 2012
Messages
11
I have this script that will go through a folder in which the path is hardcoded into the macro and consolidate all of the .csv files into the .xls I am working with utilizing Excel 2007.

Code:
Sub Consolidate()


Dim fName As String, fPath As String, fPathDone As String, OldDir As String
Dim LR As Long, NR As Long
Dim wbkOld As Workbook, wbkNew As Workbook, ws As Worksheet


    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False


    Set wbkNew = ThisWorkbook
    wbkNew.Activate
    
    'make sure the name of the first tab at the bottom of this xls file says Sheet1
    Sheets("Sheet1").Activate


    If MsgBox("Import new data to this report?", vbYesNo) = vbNo Then Exit Sub


    If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then
        Cells.Clear
        NR = 1
    Else
        NR = Range("A" & Rows.Count).End(xlUp).Row + 1
    End If


    OldDir = CurDir
    
    'select the directory path that has your csv files
    ' An example would be fPath = "C:\Documents and Settings\Administrator\My Documents\Scripts\test\"
    ' Make sure you put the backslash "\" at the end
    fPath = "\"
    
    'select the folder you want the csv files to be moved to once they are combined in the xls file
    ' An example would be fPathDone = "C:\Documents and Settings\Administrator\My Documents\Scripts\test\converted\"
    ' Make sure you put the backslash "\" at the end
    fPathDone = "\"
    
    ChDir fPath
    fName = Dir("*-*.csv")




    Do While Len(fName) > 0
            Set wbkOld = Workbooks.Open(fName)
            LR = Range("A" & Rows.Count).End(xlUp).Row
            Range("A2:A" & LR).EntireRow.Copy _
                wbkNew.Sheets("Sheet1").Range("A" & NR)
            wbkOld.Close True
            NR = Range("A" & Rows.Count).End(xlUp).Row + 1
            Name fPath & fName As fPathDone & fName
            fName = Dir
    Loop




    ActiveSheet.Columns.AutoFit
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True




    ChDir OldDir


End Sub

How can I use msoFileDialogFolderPicker to get the user to select the folder that their .csv files are located (fPath) and the folder they wish to have the files moved to once converted (fPathDone)? I think it would make things easier on the end user if they can select the folder instead of going in and manipulating the script.

Code:
    'select the directory path that has your csv files
    ' An example would be fPath = "C:\Documents and Settings\Administrator\My Documents\Scripts\test\"
    ' Make sure you put the backslash "\" at the end
    fPath = "\"
    
    'select the folder you want the csv files to be moved to once they are combined in the xls file
    ' An example would be fPathDone = "C:\Documents and Settings\Administrator\My Documents\Scripts\test\converted\"
    ' Make sure you put the backslash "\" at the end
    fPathDone = "\"
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try something like this...

Code:
    [color=green]' Prompt user to select the source folder[/color]
    [color=darkblue]With[/color] Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = "C:\Temp\"               [color=green]' Default path[/color]
        .Title = "Please Select the Source Folder"
        .ButtonName = "Select Folder"
        .AllowMultiSelect = [color=darkblue]False[/color]
        .Show
        [color=darkblue]If[/color] .SelectedItems.Count = 0 [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]   [color=green]' User clicked cancel[/color]
        fPath = .SelectedItems.Item(1) & "\"
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
    [color=green]' Prompt user to select the destination folder[/color]
    [color=darkblue]With[/color] Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = "C:\Temp\"               [color=green]' Default path[/color]
        .Title = "Please Select the Destination Folder"
        .ButtonName = "Select Folder"
        .AllowMultiSelect = [color=darkblue]False[/color]
        .Show
        [color=darkblue]If[/color] .SelectedItems.Count = 0 [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]   [color=green]' User clicked cancel[/color]
        fPathDone = .SelectedItems.Item(1) & "\"
    [color=darkblue]End[/color] [color=darkblue]With[/color]
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,298
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top