How to open a 'specific' file via 'browse'

sourabh_ajmera

New Member
Joined
Jul 17, 2014
Messages
36
Hello,

I have doubt that I had been trying to find a solution for a long time. I want to open a specific folder while the browse option is in action to prompt user to select a file to open.
I have tried the following code but the problem is its open up desktop or the last opened folder. I always want to prompt folder ABC (Location: Desktop/ABC) as a first choice for user to select file from?
Code:
Sub test()
MsgBox "Choose Your Source File to Open"
Source = Application.GetOpenFilename()
If Source = "False" Then Exit Sub
Workbooks.Open Source
End Sub

I dont know if the above statement explains my question or not. Please let me know if you have any doubt or queries.
Thank you in advance
 
Last edited:

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,456
Try using a few lines like this:

Code:
    ChDrive ("M") [COLOR=#008000]'Sets  the current drive[/COLOR]
    ChDir ("M:\Operations\Your File Directory\")[COLOR=#008000] 'Sets the current file directory[/COLOR]
    Source= Application.GetOpenFilename(FileFilter:="Excel Files ( .XLS), *.XLSX", Title:="Please pick a source reference file")
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,855
Hi

You can navigate to the drive and then to the folder and use the old GetOpenFilename() method of the Application, ...
but you can also use the more recent FileDialog property that allows you to specify the initial folder.

This is an example:

Code:
Sub GetFile()
    Dim sPathname As String
    
    Const sInitialFoldr As String = "c:\tmp\" ' Initial folder
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Please select a file"
        .InitialFileName = sInitialFoldr
        .AllowMultiSelect = False

        If .Show = False Then
            Exit Sub
        Else
            sPathname = .SelectedItems(1)
        End If
    End With
    
    MsgBox sPathname
End Sub
 

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,456
pgc01,

Thanks for the alternative method! I am always happy to see different methods. I learn something new everyday.... most of the time more then one thing :)
 

sourabh_ajmera

New Member
Joined
Jul 17, 2014
Messages
36
A-W-E-S-O-M-E!! Thanks alot guys!
Being a newbie I have learned so much that a macro can do. I kinda really like excel-VBA
 

mrmmickle1

Well-known Member
Joined
May 11, 2012
Messages
2,456
Glad to help. Just hang around the forum and you'll learn loads more :)
 

Watch MrExcel Video

Forum statistics

Threads
1,109,522
Messages
5,529,330
Members
409,863
Latest member
stacy09
Top