Avatar of John Carney
John CarneyFlag for United States of America

asked on 

Code that will open the open dialog box for a specified folder

i thought that the code below woyld bring up the open dialog box for the specified folder, but of course it doesn't. How do I do that?

Thanks,
John
ChDir "H:\Depts\css\A_ILS & Reliability\Reliability\1-CURRENT\System Reporting\SDAE\JAL\3_Working Files\SourceFiles\Originals\New Weeks"

Open in new window

Microsoft Excel

Avatar of undefined
Last Comment
zorvek (Kevin Jones)
Avatar of BlueThunda
BlueThunda
Flag of United States of America image

Something like this??

Sub Get_Data()
ChDrive "H:\"
ChDir "H:\Depts\css\A_ILS & Reliability\Reliability\1-CURRENT\System Reporting\SDAE\JAL\3_Working Files\SourceFiles\Originals\New Weeks"
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to import", _
FileFilter:="Excel Files *.xls (*.xls),")
''
If FileToOpen = False Then
MsgBox "No file specified.", vbExclamation, "Duh!!!"
Exit Sub
Else
Workbooks.Open Filename:=FileToOpen
End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of BlueThunda
BlueThunda
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of SmittyPro
SmittyPro
Flag of United States of America image

Here's an example of the GetOpenFilename method:

Sub GetOpenFileName()
    Dim FileName As Variant
    Dim Filt As String, Title As String
    Dim FilterIndex As Integer, Response As Integer
    
    '   Set Drive letter
        ChDrive "C:\"
    '   Set to Specified Path\Folder
        ChDir "C:\Documents and Settings\All Users\Desktop\"
    '   Set File Filter
        Filt = "Excel Files (*.xls), *.xls"
    '   Set *.* to Default
        FilterIndex = 5
    '   Set Dialogue Box Caption
        Title = "Please select a different File"
    '   Get FileName
        FileName = Application.GetOpenFileName(FileFilter:=Filt, _
            FilterIndex:=FilterIndex, Title:=Title)
    '   Exit if Dialogue box cancelled
        If FileName = False Then
            Response = MsgBox("No File was selected", vbOKOnly & vbCritical, "Selection Error")
            Exit Sub
        End If
    '   Display Full Path & File Name
        Response = MsgBox("You selected " & FileName, vbInformation, "Proceed")
    '   Open Selected Workbook
        Workbooks.Open FileName

End Sub

Open in new window


You'll see a lot of parameters that you can change.

Hope that helps,
Avatar of zorvek (Kevin Jones)
Chris! :-)
Avatar of kumaresan2011
kumaresan2011
Flag of India image

Hi Gaberiel

please try with code
important code lines

' File filters
Filter = "Excel Files (*.xls),*.xls,"

' Select Start Drive & Path
ChDrive ("H")
ChDir ("H:\Depts\css\A_ILS & Reliability\Reliability\1-CURRENT\System Reporting\SDAE\JAL\3_Working Files\SourceFiles\Originals\New Weeks")

' Set File Name to selected File
    Filename = .GetOpenFilename(Filter, FilterIndex, Title)

i hope this will surely helpful to you
by
kumaresan.v
Sub OpenSingleFile()
Dim Filter As String, Title As String
Dim FilterIndex As Integer
Dim Filename As Variant
' File filters
Filter = "Excel Files (*.xls),*.xls," & _
        "Text Files (*.txt),*.txt," & _
        "All Files (*.*),*.*"
' Default Filter to *.*
FilterIndex = 3
' Set Dialog Caption
Title = "Select a File to Open"
' Select Start Drive & Path
ChDrive ("H")
ChDir ("H:\Depts\css\A_ILS & Reliability\Reliability\1-CURRENT\System Reporting\SDAE\JAL\3_Working Files\SourceFiles\Originals\New Weeks")
With Application
    ' Set File Name to selected File
    Filename = .GetOpenFilename(Filter, FilterIndex, Title)
    ' Reset Start Drive/Path
    ChDrive (Left(.DefaultFilePath, 1))
    ChDir (.DefaultFilePath)
End With
' Exit on Cancel
If Filename = False Then
    MsgBox "No file was selected."
    Exit Sub
End If
' Open File
Workbooks.Open Filename
MsgBox Filename, vbInformation, "File Opened" ' This can be removed
End Sub

Open in new window

SOLUTION
Avatar of kumaresan2011
kumaresan2011
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of John Carney
John Carney
Flag of United States of America image

ASKER

Just got into work, I'll check this out as soon as I can.


- John
Avatar of John Carney
John Carney
Flag of United States of America image

ASKER

Well the day got away from me pretty fast. I'm emailing myself to check it out first thing tomorrow :-)

John
Avatar of John Carney
John Carney
Flag of United States of America image

ASKER

Thanks.
Avatar of John Carney
John Carney
Flag of United States of America image

ASKER

Sorry I didn't get back sooner since BlueThunda's posts answered the question completely. I gave some points to kumaresan2011 because his answer opened up some possibilities I was thinking about.

SmittyPro, pls forgive me for not including you but your answer was pretty much a duplicate of BlueThunda's and of course he got there first. But please take a look at a followup question I posted here: https://www.experts-exchange.com/questions/27044789/Opening-all-files-in-a-given-folder.html. Anyone else as well.

Kevin, your answer as always was slyly brilliant in a zen master sort of way; so much so that many people might fail to recognize it as such.  You never disappoint. :-)

John
Avatar of SmittyPro
SmittyPro
Flag of United States of America image

Kevin never ceases to amaze! :)

Heya!
I really appreciate the adoration and appreciation, but, truth be told, I was just saying hello and acknowledging my buddy Chris AKA SmittyPro. He doesn't show up often so I wanted him to know I saw his happy self. He does happen to be pretty good at doing whatever it is he decides to do on any given day - from code to teaching to botany.

:-)
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo