Select file to open within the same folder

Jim885

Well-known Member
Joined
Jul 8, 2012
Messages
663
I have the below code that works well, but need to modify it so that it starts within the same folder instead of the default of the My Documents folder.
Is there a way to have this open within the same folder?

I don't want to specify the file path. Rather use something like ThisWorkbook.Path

Please help.

VBA Code:
Dim FileToOpen As Variant
Dim FileName As String
Dim OpenBook As Workbook

Application.ScreenUpdating = False

FileToOpen = Application.GetOpenFilename(Title:="Browse for the file to Import", FileFilter:="Excel Files(*.xls*),*xls*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Active.Worksheet.Unprotect
OpenBook.Sheets(1).Range("A1:E20").Copy
ThisWorkbook.Worksheets("Template").Range("A2").PasteSpecial xlPasteValues
OpenBook.Close False
End If

Application.ScreenUpdating = True
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Use FileDialog instead of GetOpenFilename...

VBA Code:
    Dim FileToOpen As String
  
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "Excel Files", "*.xls*"
        .InitialFileName = ThisWorkbook.Path & "\"
        .Title = "Browse for the file to Import"
        .Show
        If .SelectedItems.Count > 0 Then
            FileToOpen = .SelectedItems(1)
        End If
    End With
  
    If Len(FileToOpen) > 0 Then
        'etc
        '
        '
        '
    End If

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,834
Members
449,051
Latest member
excelquestion515

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