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
 

Some videos you may like

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,455
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:

Watch MrExcel Video

Forum statistics

Threads
1,127,031
Messages
5,622,302
Members
415,891
Latest member
Oksana88

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
Top