Below is a macro I recorded for a project that I am working on. There is a new text file that comes weekly (in this example the file name is HMO04102015.txt
) and I am wondering whether there is a way (maybe an additional macro that I can put into the code) that I can pull the new weekly file name and replace it automatically (maybe like a "pull most recent time stamp" function). The files come on the same day of the week so I know what day they are coming (if that helps at all).
Sub textimport()
'
' textimport Macro
'
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\Employee\Desktop\Test Data\HMO04102015.txt", _
Destination:=Range("$A$1"))
.Name = "HMO04102015"
.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 = xlFixedWidth
.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)
.TextFileFixedColumnWidths = Array(11, 29, 9, 7, 2, 3, 167, 51, 39, 18)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("F:F").Select
Selection.Delete Shift:=xlToLeft
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Columns("A:A").EntireColumn.AutoFit
Selection.ColumnWidth = 27
Range("B2").Select
End Sub