Good morning,
I've included my VBA code below which I am running to attempt to programmatically navigate to a folder and then open all .CSV files (and also ignoring all non-.CSV files) and perform a Save As function so that the files are saved as .XLSM. I have hundreds of individual .CSV files in this folder to convert to .XLSM files (and receive additional files on a daily basis that must also be converted) and if properly executed, it will save me a ton of time and will mitigate errors caused by manually performing this operation.
I can't seem to figure out the problem here. I've stepped through the code and I can clearly see each of my .CSV files in the targeted folder being opened and then properly closed, but the targeted files remain .CSV files instead of being changed to .XLSM files. I do not see any error messages when I run this code.
Any assistance is greatly appreciated!
Option Explicit
Sub ConvertToXLSM()
'
' Uses code from John Walkenbach's Power Programming book
'
Dim i As Long
Dim NumFiles As Long
Dim FileName As String
Dim FileNames() As String
' Get name of first file in backlog directory
FileName = Dir(ThisWorkbook.Path & "/*.csv")
NumFiles = 1
ReDim Preserve FileNames(1 To NumFiles)
FileNames(NumFiles) = FileName
' Get other file names, if any
Do While FileName <> ""
FileName = Dir()
If FileName <> "" Then
NumFiles = NumFiles + 1
ReDim Preserve FileNames(1 To NumFiles)
FileNames(NumFiles) = FileName
End If
Loop
' Save each file as a .xlsm file, overwriting any existing .xlsm Files
Application.DisplayAlerts = False
For i = 1 To UBound(FileNames)
If FileNames(i) <> ThisWorkbook.Name Then
Workbooks.Open FileName:=ThisWorkbook.Path & "\" & FileNames(i)
ActiveWorkbook.SaveAs FileName:=Left(FileNames(i), Len(FileNames(i)) - 4) & ".xlsm", FileFormat:=52
ActiveWorkbook.Close
End If
Next i
Application.DisplayAlerts = True
End Sub
Bookmarks