Dynamic Folder directory

jabawalkie5000

New Member
Joined
Jun 13, 2011
Messages
41
Hi All

I am trying to include coding in a macro that enables the user to enter the path in the control tab of my report.

The macro can then read that string and go to the desired folder to open the selected files

Can anyone help?

Many Thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Try like this

Code:
Sub GetFileName()
Dim fName As Variant
fName = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLS), *.XLS", Title:="Select File To Be Opened")
If fName = False Then Exit Sub
Workbooks.Open Filename:=fName
End Sub
 
Upvote 0
If you just want to select the folder rather than a file:

Code:
Dim strFolder As String

With Application.FileDialog(msoFileDialogFolderPicker)
  .AllowMultiSelect = False
  .InitialFileName = "C:\SomeFolder\"  'default start location
  .Title = "Please select a folder"
  If .Show = -1 Then strFolder = .SelectedItems(1) Else Exit Sub
End With

MsgBox "Path of folder is " & strFolder
 
Upvote 0
Thanks again

Here is where I stand, I have a macro that has a static folder directory within, it goes to that folder, imports all the file names including extensions to a tab called "sheet1".

But I only want it to pick up .doc, .xls & .ppt files


Can you please tweak your macro to make the code dynamic and only import the 3 file types to my tab called sheet1?

Thanks
 
Upvote 0
Yes it is, I will incorporate that into the macro, so hopefully the user doesnt have to go cutting and pasting folder directory strings

Thanks, please see my new query above

Isn't it easier for them to use a dialog to select a file or folder?
 
Upvote 0
Perhaps you mean something like

Code:
Sub ListFiles()
Dim MyFolder As String
Dim MyFile As String
Dim j As Integer
MyFolder = Sheets("control").Range("A1").Value
MyFile = Dir(MyFolder & "\*.*")
Do While MyFile <> ""
    If MyFile Like "*.doc" Or MyFile Like "*.xls" Or MyFile Like "*.ppt" Then
        j = j + 1
        Sheets("Sheet1").Cells(j, 1).Value = MyFile
    End If
    MyFile = Dir
Loop
End Sub
 
Upvote 0
Sorry to mess you around, could you now incorporate a dialogue box so that the user can just browse easily for the path and the rest of your macro works as per below?

Many Many Thanks

Perhaps you mean something like

Code:
Sub ListFiles()
Dim MyFolder As String
Dim MyFile As String
Dim j As Integer
MyFolder = Sheets("control").Range("A1").Value
MyFile = Dir(MyFolder & "\*.*")
Do While MyFile <> ""
    If MyFile Like "*.doc" Or MyFile Like "*.xls" Or MyFile Like "*.ppt" Then
        j = j + 1
        Sheets("Sheet1").Cells(j, 1).Value = MyFile
    End If
    MyFile = Dir
Loop
End Sub
 
Upvote 0
Combining mine and Richard's

Code:
Sub ListFiles()
Dim MyFolder As String
Dim MyFile As String
Dim j As Integer
With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    .InitialFileName = "C:\"  'default start location
    .Title = "Please select a folder"
    If .Show = -1 Then
        MyFolder = .SelectedItems(1)
    Else
        Exit Sub
    End If
End With
MyFile = Dir(MyFolder & "\*.*")
Do While MyFile <> ""
    If MyFile Like "*.doc" Or MyFile Like "*.xls" Or MyFile Like "*.ppt" Then
        j = j + 1
        Sheets("Sheet1").Cells(j, 1).Value = MyFile
    End If
    MyFile = Dir
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

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