VBA - dat file and loop to get certain cells

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


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
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,215,219
Messages
6,123,678
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top