954,558 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Hoping for Help - Import File into Excel

Hi,

I'm trying to use a variable to import a text file. Is it possible to change the path and filename below to a variable? I would like to change "L:\GEAM Data.txt" to a variable "fname", which a user previously entered using Application.GetOpenFilename.

With ActiveSheet.QueryTables.Add _
    (Connection:="TEXT;L:\GEAM Data.txt", _
    Destination:=Range("A1"))


Any help would be greatly appreciated as I've tried every syntax combination I could think of.

Thanks,
Mike

mikefromchitown
Newbie Poster
4 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

If I'm not mistaken (yeah, sometimes, but not frequently ;) ) this should work:

Fname = Application.GetOpenFilename
With ActiveSheet.QueryTables.Add (Connection:="TEXT;" & Fname, Destination:=Range("A1"))
end with


Now, I've tried the code, and maybe my excel VBA Coding is a bit rusty... but I don't get it loading into any cells that way. I think the file will have to be opened through code, read in, and put into the cells..... but let me know if the above works for you, and maybe I can figure out why it doesn't for me :eek:

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

It worked for me! Here's the total code (with your change) that worked. Thanks much!!!!

Dim fname As String
fname = Application.GetOpenFilename
      
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fname, Destination:=Range("A1"))
     .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 = True
     .TextFileSemicolonDelimiter = False
     .TextFileCommaDelimiter = False
     .TextFileSpaceDelimiter = False
     .Refresh BackgroundQuery:=False
End With
mikefromchitown
Newbie Poster
4 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

Sweet! ;)

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Not really wishing to drag up the past but this little snipit of code is what i have been looking for all day, thanks very much.

Bogar
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

in case user press cancel,
put this code:
if fname= false then exit sub

ibancracker74
Newbie Poster
1 post since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You