change macro to be able to choose folder

Jos1972

New Member
Joined
Nov 3, 2008
Messages
33
Hello,
If found below macro that helps me to list the file names in a folder. What i would like to change is to have possibility to choose a folder instead of a fixed one. Can anyone help me?
thanks
jos


Sub ListAllFile()

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim ws As Worksheet

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ws = Worksheets.Add

'Get the folder object associated with the directory
Set objFolder = browseforfolder 'objFSO.GetFolder("C:\")

ws.Cells(1, 1).Value = "The files found in " & objFolder.Name & "are:"

'Loop through the Files collection
For Each objFile In objFolder.Files
ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = objFile.Name
Next

'Clean up!
Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Add this code to a new standard code module:-
Code:
Option Explicit
 
Public Function BrowseForFolder(Optional ByVal StartFolder As Variant)
 
  If IsMissing(StartFolder) Then StartFolder = ""
 
  If StartFolder <> "" Then
    If Right(StartFolder, 1) <> "\" Then StartFolder = StartFolder & "\"
  End If
  With Application.FileDialog(msoFileDialogFolderPicker)
    .InitialFileName = StartFolder
    .Title = "Please choose a folder:-"
    .AllowMultiSelect = False
    If .Show = -1 Then BrowseForFolder = .SelectedItems(1)
  End With
 
End Function
and modify your code as follows:-
Code:
[COLOR=green]'Get the folder object associated with the directory[/COLOR]
Dim SelectedFolder As String
SelectedFolder = BrowseForFolder
If SelectedFolder = "" Then
[COLOR=green] ' do whatever you need to do when user cancels out of dialog box[/COLOR]
  MsgBox "User cancelled!" & Space(10), vbOKOnly + vbExclamation
  Exit Sub
End If
Set objFolder = objFSO.GetFolder(SelectedFolder)
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,550
Members
452,927
Latest member
rows and columns

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