FileToOpen From Specific Directory

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code below which allows a user to select a file. How can I modify it so that the selection comes from a specific directory?

Code:
    FileToOpen = Application.GetOpenFilename(Title:="Raw ActiveNet Files", FileFilter:="active_report_(*.xls*),*xls*")
    
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        ActiveWindow.Visible = False
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
This is how I do it.

VBA Code:
PSRPath = SETUP.Range("PSRTxtFileLoc").Value
  ChDrive Left(PSRPath, 3)
  ChDir PSRPath
    
  v = Application.GetOpenFilename("Text Files (*.txt), *.txt", 1, "Select the Period Structure Text File")
  If v = False Then Exit Sub
 
Upvote 1
How about

VBA Code:
Sub jec()
 Dim wb
 With Application.FileDialog(3)
   .Filters.Clear
   .Filters.Add "Excel Files", "*.xlsx; *.xlsm"
   .InitialFileName = "C:\Users\xxxxx\Downloads\"
    If .Show = 0 Then Exit Sub
    Set wb = Workbooks.Open(.SelectedItems(1))
 End With
End Sub
 
Upvote 1
Solution
Thank you both for your support.

Jeffrey, it's possible I misunderstood how your code work. It looked to me that it was as simple as just changing the directory, however, with this assumption, I wasn't able to get that to happen.
Code:
    anpath = "D:\Projects\AppSheet Sports 2024\Raw_Activenet\"
    Application.ScreenUpdating = False
    ChDir anpath

JEC, your solution did provide the results while introducing me to a new process (File Dialogue). I will have to Google it to understand it further.
 
Upvote 0
If the drive is not the same as the default, you'll need to change that with ChDrive

CHDrive "D:\"
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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