VBA fails to limit to just .dat files if there are zip files present in the folder

54 Views Asked by At

I have added a filter to the type of files allowed to selected (.dat files) however I have found that if there are zip files present in the folder the script does not work correctly as it also opens the zip files.

'Specify folder where measurement files are stored
With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show = True Then
    MyFolder = .SelectedItems(1)
    Else
        Exit Sub
    End If
End With

Sheet8.Columns.ClearContents

sFile_Name = Dir(MyFolder & "\*.dat*")
Do While sFile_Name <> ""
    Set wsData_Sheet = Sheet5
    j = wsData_Sheet.Cells(1, Columns.Count).End(xlToLeft).Column
    jn = j + 1
    lcl = Sheet8.Cells(2, Columns.Count).End(xlToLeft).Column
    lclp = lcl + 1

    Call PARSE_MDF(MyFolder & "\" & sFile_Name, jn, sEND, lclp)
    sFile_Name = Dir()
Loop

How do I exclude zip files and limit it to only .dat files?

1

There are 1 best solutions below

0
On BEST ANSWER

I'm guessing that your zip files may also hold the string .dat somewhere in them (such as file.dat.zip for example) because this snippet:

sFile_Name = Dir(MyFolder & "\*.dat*")
'                    note this bit ^

will give you all files containing .dat, not all those ending in .dat.

If you only want the latter, try:

sFile_Name = Dir(MyFolder & "\*.dat")

(without the final *).