Macro for importing user selected text file

Quansone

New Member
Joined
May 22, 2018
Messages
1
New to macros so am feeling pretty dangerous.

I'm trying to automate importing txt files and saving as .xlsx which I am able to record macro that imports a specific file and saves it accordingly.

However now trying to make it a bit smarter in that I'd like the user to be prompted to select the txt file to import, macro then converts and then subsequently saves as a .xlsx file with dynamic name (file name exists in excel or saved as the name of the txt file imported).

Below is macro recorded for importing and saving specific file.


Sub Pac_Steel_Import()

Range("A2").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;U:\My Documents\That Place\Y810168566.TXT", Destination:=Range( _
"$A$2"))
.Name = "Y810168566"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Selection.End(xlDown).Select
Selection.ClearContents
Rows("2:2").Select
Selection.ClearContents
Range("D3").Select
ActiveCell.FormulaR1C1 = "Y810168566"
ChDir "\\Here\there\everywhere\My Documents\That Place"
ActiveWorkbook.SaveAs Filename:= _
"\\here\there\everywhere\My Documents\That Place\Y810168566.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,215,394
Messages
6,124,683
Members
449,180
Latest member
kfhw720

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