Hello,
I have the following code, which runs correctly, however, it is skipping every other file in my directory. There are 66 files and it ends up converting 34 (1 file has 2 sheets that are converted as separate csv's). Does anyone know why it would be doing that?
Sub SaveToCSVs()
Dim fDir As String
Dim wb As Workbook
Dim wS As Worksheet
Dim csvWs As String, csvWb As String
Dim extFlag As Long '0 = .xls & 1 = .xlsx extension types
Dim fPath As String
Dim sPath As String, dd() As String
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
fPath = "C:\Data\*.*"
sPath = "C:\Data"
fDir = Dir(fPath)
extFlag = 2
Do While (fDir <> "")
If Right(fDir, 4) = ".xls" Or Right(fDir, 5) = ".xlsx" Then
extFlag = 0
Else
extFlag = 2
End If
On Error Resume Next
If extFlag = 0 Then
fDir = Dir
Set wb = Workbooks.Open(sPath & fDir)
csvWb = wb.Name
dd = Split(csvWb, ".")
For Each wS In wb.Sheets
If Right(wS.Name, 8) = "Criteria" Then
'Do nothing
Else
wS.SaveAs sPath & dd(0) & "-" & wS.Name & ".csv", xlCSV
End If
Next wS
wb.Close False
Set wb = Nothing
fDir = Dir
On Error GoTo 0
End If
Loop
'Reset Macro Optimization Settings
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
I have the following code, which runs correctly, however, it is skipping every other file in my directory. There are 66 files and it ends up converting 34 (1 file has 2 sheets that are converted as separate csv's). Does anyone know why it would be doing that?
Sub SaveToCSVs()
Dim fDir As String
Dim wb As Workbook
Dim wS As Worksheet
Dim csvWs As String, csvWb As String
Dim extFlag As Long '0 = .xls & 1 = .xlsx extension types
Dim fPath As String
Dim sPath As String, dd() As String
'Optimize Macro Speed
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
fPath = "C:\Data\*.*"
sPath = "C:\Data"
fDir = Dir(fPath)
extFlag = 2
Do While (fDir <> "")
If Right(fDir, 4) = ".xls" Or Right(fDir, 5) = ".xlsx" Then
extFlag = 0
Else
extFlag = 2
End If
On Error Resume Next
If extFlag = 0 Then
fDir = Dir
Set wb = Workbooks.Open(sPath & fDir)
csvWb = wb.Name
dd = Split(csvWb, ".")
For Each wS In wb.Sheets
If Right(wS.Name, 8) = "Criteria" Then
'Do nothing
Else
wS.SaveAs sPath & dd(0) & "-" & wS.Name & ".csv", xlCSV
End If
Next wS
wb.Close False
Set wb = Nothing
fDir = Dir
On Error GoTo 0
End If
Loop
'Reset Macro Optimization Settings
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub