I made a macro where when I click a button, a text file is imported from the data tab (import data from text), and then some formatting is done to the text. When I recorded the Macro I simply hit record and manually went to the data tab and imported a text file. Then I assigned the macro to the button.
However, I intend to use this macro for different files with different names and from different locations, but the macro only opens the very file I opened when recording it. If that file is not available I get an error message.
Instead of instantly importing my file, I want a file dialog box to appear when I click the button, so I then can browse for any text file to import, and have the formatting done to.
This is what my code looks like now. Any advice?
("C:\Users\u0137477\Desktop\Macro test\DetailLog11.txt" is the file I used when recording the macro.)
However, I intend to use this macro for different files with different names and from different locations, but the macro only opens the very file I opened when recording it. If that file is not available I get an error message.
Instead of instantly importing my file, I want a file dialog box to appear when I click the button, so I then can browse for any text file to import, and have the formatting done to.
This is what my code looks like now. Any advice?
("C:\Users\u0137477\Desktop\Macro test\DetailLog11.txt" is the file I used when recording the macro.)
Code:
Sub Macro1()'
' Macro1 Macro
'
'
Sheets.Add After:=Sheets(Sheets.Count)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\u0137477\Desktop\Macro test\DetailLog11.txt", Destination:= _
Range("$A$1"))
.Name = "DetailLog11"
.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 = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileDecimalSeparator = "."
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
End With
ActiveWindow.FreezePanes = True
Rows("1:1").Select
Selection.Font.Bold = True
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C1").Select
ActiveCell.FormulaR1C1 = "Time (hh:mm:ss)"
Range("C2").Select
Columns("C:C").EntireColumn.AutoFit
Columns("C:C").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("C2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/86400"
Range("C2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/86400"
Selection.AutoFill Destination:=Range("C2:C14134")
Range("C2:C14134").Select
Selection.NumberFormat = "h:mm:ss"
Columns("B:B").Select
Selection.EntireColumn.Hidden = True
Columns("A:A").ColumnWidth = 34.29
Columns("A:A").Select
Selection.ColumnWidth = 27.29
Range("A1").Select
End Sub