how to open a browse dialog with VBA

bdee1

Board Regular
Joined
Feb 17, 2003
Messages
105
i have a sheet that runs a macro which imports a comma delimited file into a sheet.

right now it grabs the file fromthe location which i have hard coded. i woudl like for there to be a way for the user to be presented with a browse dialog when the macro runs so they can locate the file to be imported.

how would i do this?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Use the GetOpenFileName method...

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> OpenFile()
    <SPAN style="color:#00007F">Dim</SPAN> fileName
    
    fileName = Application.GetOpenFilename("Comma Separated Values (*.csv),*.csv")
    
    <SPAN style="color:#00007F">If</SPAN> fileName <> "False" <SPAN style="color:#00007F">Then</SPAN>
        Workbooks.<SPAN style="color:#00007F">Open</SPAN> fileName, Format:=2
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 
Upvote 0
wow - thanks for the quick reply! it worked great! but i do have one more question.

instead of just opening the filename retreived by the browse dialog, i have to run a macro which imports the data (stripping out some rows). so the code i used is below:

but the problem is that when i run it, it imports the data and then gives me an error saying:

"Run- time error '1004':
Excel cannot find the text file to refresh this external data range.
Check to make sure the text file has not been moved or renamed, then try the refresh again."


the code i am using is as follows:

Code:
Sub Get_eRehabData()
'
' Get_eRehabData Macro
' Macro recorded 2/11/2004 by BDEE1
'

'
   Dim fileName
   fileName = Application.GetOpenFilename("eRehab Downloads (*.out),*.out")

  If fileName <> "False" Then
    Sheets("eRehab_Download").Select
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;fileName" _
        , Destination:=Range("A1"))
        .Name = "HIPPS_333025_040211_142737_105"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(9, 9, 1, 1, 1, 9, 5, 9, 9, 5, 9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, _
        1, 1, 9, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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