Use .GetOpenFilename without actually opening the selected file

klimax

New Member
Joined
Feb 23, 2016
Messages
2
Hello,

New member here, so i feel a small introduction is in order:
I am an engineer residing in Kentucky, born and raised in Chicago, IL.
Right now the current purposes of my excel tasks are for work, in where I am developing a number of applications to assist my company in simplifying various routine data analysis tasks.
I am relatively new to VBA code in general, and the extent of my knowledge is entirely self-taught using resources such as this forum.

My question:

I am working on writing a string of code to allow a user to select a file (.CSV) to be used in further parts of the sub.
Right now I have been able to use .GetOpenFilename to select a file, and import the selected .csv file into a fixed sheet.
However, the only issue i have with this method is that i really do not need the file selected to open, as i just need the path for further processes.

So, is there a way i can modify my code to prevent the selected file from opening all together while still using .GetOpenFilename or is there some other method that will accomplish what i need?

I have searched through the various posts regarding .GetOpenFilename with no luck, so i figure that i am probably just using .GetOpenFilename when i should be using something else.


Code:
    Dim Filename 
   
'Browse for .CSV file to analyze
    Filename = Application.GetOpenFilename( _
    "Comma Separated Values (*.csv),*.csv", _
    Title:="Select CSV File")


    If Filename <> "False" Then
    
    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With


    Workbooks.Open Filename, Format:=2
    End If


' Further on, i use "Filename" in the string to import the CSV as such. 
' "Dump" is us the sheet it sends to, i have omitted some code 
        'import data from CSV file selected
        With Dump.QueryTables.Add(Connection:= _
        "TEXT;" & Filename, Destination:=Dump.Range("$A$1"))
        .Name = "test"
        .FieldNames = True
        ' middle code omitted 
        .Refresh BackgroundQuery:=False
        End With

Like i said, everything works without issue, but i just want to prevent the file selected from actually opening.
(However I still have yet to add some code to exit sub when user cancels the file selection window)

Thank you
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
GetOpenFilename doesn't open the file, all it does it get the name.

So to do what you want remove the code that opens the file, ie this.
Code:
    Workbooks.Open Filename, Format:=2
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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