Hi all,
I have recorded some script in excel to import a *.dat file, delete the first 19 rows of data, then export the data as a *.csv file.
My problem is I have circa 30 *.dat files to import, all of which have large file names and I am fed up with copying and pasting them in.
So my question is, in the below code, can I somehow use a reference at the start such as:
abc1.dat = "1"
abc2.dat = "2"
Then where the files were used, reference the "1".dat and its exported version "1".csv for the abc1.dat case.
Sub Import_dat_Modify_Export_csv()
'
'
' Macro to save *.dat files in *.csv format while removing the un-needed data at the top of the *.dat files
'1
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;D:\TEMP\DIR\abc1.dat" _
, Destination:=Range("A1"))
.Name = "abc1"
.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 = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Rows("1:19").Select
Selection.Delete Shift:=xlUp
Range("A1").Select
ChDir "D:\TEMP\X123"
ActiveWorkbook.SaveAs Filename:= _
"D:\TEMP\X123\abc1.csv" _
, FileFormat:=xlCSV, CreateBackup:=False
ActiveWindow.Close
Workbooks.Add
'2
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;D:\TEMP\DIR\abc2.dat" _
, Destination:=Range("A1"))
.Name = "abc2"
.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 = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Rows("1:19").Select
Selection.Delete Shift:=xlUp
Range("A1").Select
ChDir "D:\TEMP\X123"
ActiveWorkbook.SaveAs Filename:= _
"D:\TEMP\X123\abc2.csv" _
, FileFormat:=xlCSV, CreateBackup:=False
ActiveWindow.Close
Workbooks.Add
End Sub
Appologies as I am somewhat *poor* at VBA code - hence my use of the Recorder.
Cheers
Bookmarks