george hart
Board Regular
- Joined
- Dec 4, 2008
- Messages
- 241
All
I'm trying to open a dat file, loop through it to get data in column "A" which reads "CC" and place in a Spreadsheet. I have the following code but falls over at "Set wb = Workbooks.OpenText(filename:=(folderPath & filename),....".
Any help most appreciated
I'm trying to open a dat file, loop through it to get data in column "A" which reads "CC" and place in a Spreadsheet. I have the following code but falls over at "Set wb = Workbooks.OpenText(filename:=(folderPath & filename),....".
Any help most appreciated
Code:
Sub Get_Files()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook
folderPath = ThisWorkbook.Path & "\" & "Imports" & "\"
strPath = ThisWorkbook.Path & "\" & "Archive" & "\"
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*.dat")
If filename = "" Then MsgBox "The Imports folder is empty, you need the appropriate Dat files in order to run this Macro", vbInformation, "Emtpy Folder Alert"
If filename = "" Then Exit Sub
Do While filename <> ""
Set wb = Workbooks.OpenText(filename:=(folderPath & filename), Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlNone, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma _
:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, _
1), Array(3, 2), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1)), _
TrailingMinusNumbers:=True)
Call Get_Data
strFileName = Format(Now(), "yyyy-mm-dd") & "_" & ActiveWorkbook.Name & "CSV"
wb.SaveAs filename:=strPath & strFileName, FileFormat:=xlCSV
wb.Close SaveChanges:=False
filename = Dir
Loop
Kill folderPath & "*.Dat"
End Sub