Using FolderObj = FileSystemObj.GetFolder("Folderpath") the Folder path as through Folder picker

Shaan Bhasin

New Member
Joined
Jan 15, 2015
Messages
4
Hi All,

I want to use the below line in my excel vba :
FolderObj = FileSystemObj.GetFolder("Folderpath")

However i want the Folder path to be Dynamic that's it will ask the user to choose the folder through Folder picker or some other command.

Any help how i can achieve the same.

Thanks in adavnce.
Shaan
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Something like this:
Code:
Public Sub ToUse()

    Dim FolderObj As Object
    Dim FolderPath As String
    
    'FolderPath = CStr(GetFolderString())  'This will use ThisWorkbook.Path as starting folder.
    'OR
    FolderPath = CStr(GetFolderString("C:\Documents and Settings"))
    
    If FolderPath <> "" Then
        Set FolderObj = GetFolderObject(FolderPath)
    End If
    
    
End Sub


Public Function GetFolderObject(FolderPath As String) As Object
    
    Dim mFSO As Object
    Set mFSO = CreateObject("Scripting.FileSystemObject")
    
    Set GetFolderObject = mFSO.GetFolder(FolderPath)
    
End Function


Function GetFolderString(Optional startFolder As Variant = -1) As Variant
    Dim fle As Object
    Dim vItem As Variant
    
    Set fle = Application.FileDialog(4)
    With fle
        .Title = "Select a folder"
        .AllowMultiSelect = False
        If startFolder = -1 Then
            .InitialFileName = ThisWorkbook.Path
        Else
            If Right(startFolder, 1) <> "\" Then
                .InitialFileName = startFolder & "\"
            Else
                .InitialFileName = startFolder
            End If
        End If
        If .Show <> -1 Then GoTo NextCode
        vItem = .SelectedItems(1)
    End With
NextCode:
    GetFolderString = vItem
    Set fle = Nothing
End Function
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,085
Members
452,378
Latest member
Hoodzy01

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