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
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