Open a specific folder using FilePicker

tigerdel

Board Regular
Joined
Oct 13, 2015
Messages
145
Office Version
  1. 365
Platform
  1. Windows
Good evening

I have code that opens the FilePicker to allow the user to open a file

What I would like to do is to have the FilePicker open a specific folder in OneDrive using the Environ call so it doesn't matter who is using it it goes to their folder

In this case I want it to open: "Environ$("OneDrive")" & "MJM Services\4.0 Accounts\2.0 Bank Statement Downloads"

The code I have [which works but doesn't go to the folder] is:

Code:
Dim fullpath As String, Cn As Variant, lastRow As Long
Call Clear_Raw_Data
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Title = "Select the file to import..."
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.csv", 1
        .Show
        fullpath = .SelectedItems.Item(1)
    End With
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    With Worksheets("Raw Data").QueryTables.Add( _
        Connection:="TEXT;" & fullpath, _
        Destination:=Worksheets("Raw Data").Range("$A$2"))
        .Name = "Period Data"
        .FieldNames = False
        .TextFileStartRow = 2
        .TextFileCommaDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .Refresh BackgroundQuery:=False
    Columns("A:A").Select
    Selection.NumberFormat = "m/d/yyyy"
    End With
    For Each Cn In ThisWorkbook.Connections
        Cn.Delete
    Next Cn
    For Each Cn In ActiveSheet.QueryTables
        Cn.Delete
    Next Cn
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

Any help gratefully received
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You can use the InitialFileName property of the FileDialog object to specify the folder to display initially. By the way, you should also handle situations where a user clicks on Cancel. Try the following...

VBA Code:
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .InitialFileName = Environ$("OneDrive") & "\MJM Services\4.0 Accounts\2.0 Bank Statement Downloads\"
        .Title = "Select the file to import..."
        .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.csv", 1
        If .Show <> -1 Then Exit Sub 'user clicked Cancel
        fullpath = .SelectedItems.Item(1)
    End With

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,877
Messages
6,122,051
Members
449,064
Latest member
scottdog129

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