Using user selected folder as variable

ramsola

New Member
Joined
Aug 15, 2007
Messages
5
Hi,

I am looking for a way to have the user select a folder where multiple files reside and have this filepath somehow recorded as a value in the code which will be refered to at various locations within the code. I need this because the same file structure will be in each folder but the folder name/filepath will change.

Can anyone help, please?

Ramsola
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi Ramsola

Something like this should work:

Code:
Dim FileLocation As String

With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = False
    .Show
    
    If .SelectedItems.Count = 0 Then
        'user pressed cancel
        MsgBox "you pressed cancel"
    Else
        FileLocation = .SelectedItems(1)
        'remove the line below once you are happy testing this
        MsgBox FileLocation
    End If
    
End With

HTH, Andrew

P.S. While inside the With section, if you press the period key (.) you will see a number of other options appear - for example, you can set the dialogue box title, set a filter to only show certain types of files and so forth.
 
Upvote 0

Forum statistics

Threads
1,214,667
Messages
6,120,810
Members
448,990
Latest member
rohitsomani

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