Capture name of data file when importing into another file

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
72,169
Office Version
  1. 365
Platform
  1. Windows
I am using the following code to browse to a CSV file and import it into my current workbook. How can I “capture” the name of the text file (myFile) for future use?

Code:
    myFile = Application.GetOpenFilename("All Files,*.csv")
    If myFile = False Then
        Exit Sub
    End If
    
'   Open file
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & myFile, Destination:=Range("A1"))
        .Name = "DATA"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = xlWindows
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = "|"
        .TextFileColumnDataTypes = Array(1)
        .Refresh BackgroundQuery:=False
    End With
Thanks
 

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.
Don't you already have it in the variable myFile?
 
Upvote 0
Don't you already have it in the variable myFile?
Its not a string. It must be some sort of object.

I tried:
Code:
Range("A1")=myFile
and
Code:
MsgBox myFile
and
Code:
MsgBox myFile.Name
but none of them worked.
 
Upvote 0
It's not an object, it's a string.

That is unless you've hit Cancel or selected multiple files, and I don't think the code will allow multiple selections as it is.
 
Upvote 0
Have you actually tried it yourself?
Can you get the file name to appear in a Message Box or be written to a cell in the file?
If you have, please post the code.
 
Upvote 0
Yes I did try it.

The only thing I changed was the file extension - don't have any CSVs kicking around.

Mind you I probably do but Vista's hiding them from me.:eek:
 
Upvote 0
Tested using a CSV file

Code:
Sub test()
Dim MyFile
MyFile = Application.GetOpenFilename("All Files,*.csv")
    If MyFile = False Then
        Exit Sub
    End If
MsgBox TypeName(MyFile)
MsgBox MyFile
End Sub

MyFile is returned as type String and contains the full path to the file.
 
Upvote 0
I think my computer is messing with me, and trying to make me look bad. I tried VoGII's code, and it worked. Then I tried the code that I posted earlier that I had commented out because it errored out, and it also works now.

I can't explain why it didn't work before, but it works now, so I guess "all is well that ends well".

Thanks to the both of you.
 
Upvote 0
No problem.:)

If you think your computer is messing with you, you'll not believe the problems I've been having since getting a new machine with Vista as the OS.:eek:

Couple of days ago I lost thousand of files that I'd transferred from the old hard drive.:eek:

Still got the old hard drive though.:)
 
Upvote 0
Yeah, I have been having a number of wacky things since I got this new laptop, especially with Copy/Paste. For some reason, it doesn't like CTRL-C and CTRL-V sometimes.
 
Upvote 0

Forum statistics

Threads
1,214,570
Messages
6,120,296
Members
448,954
Latest member
EmmeEnne1979

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