Importing text files with varying names

johenry

New Member
Joined
Jun 20, 2008
Messages
4
Hello - I am trying to create a macro that allows me to import a text file via the import wizard. However, when I record the macro, I don't know how to modify the code so that the macro prompts me for the file name before importing. This would allow me to use this macro to open different file names. (the current file name is "PODetail_Orthopedic.txt")

Sample of recorded macro code:

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\johenry\Documents\Ascension\Client Data\Spend\PO Data\PODetail_ORTHOPEDIC.txt" _
, Destination:=Range("$B$7"))
.Name = "PODetail_ORTHOPEDIC"
.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 = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Something like this:

Code:
    Dim fileLookup As String
    Dim fileToOpen As String
    fileLookup = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    fileToOpen = "TEXT;" & fileLookup
    With ActiveSheet.QueryTables.Add(Connection:=fileToOpen, Destination:=Range("A1"))

And then all your .Name, etc follows in the With block
 
Upvote 0
Yes - the file names will change.

Thanks! That does work. One question, however. I still have the bit of code that reads:

.Name = "PODetail_ORTHOPEDIC"

in the code - will this cause any unintended affects as it is no longer the name of the file? At first blush it did not seem to, but I thought I would ask.
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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