Forum Discussion

lnmlilo's avatar
lnmlilo
Copper Contributor
Nov 14, 2024

VBA: Save file as .csv with sequential numbering

Good day Community,

 

I am trying to save one sheet from a macro-enabled file as a .csv document. I will need to save it every month so each month I would like to save the file with a new number (e.g. Test File 1.csv; Test File 2.csv; Test File 3.csv etc.).

I have tried the following code but it always gets stuck at the part that I have highlighted in yellow below and I get the attached error message. Any ideas on how to move past this would be appreciated!

(I am using Excel on Mac)

 

Sub SaveSheetAsCSVWithSequence()     

Dim ws As Worksheet    
Dim filePath As String    
Dim fileName As String    
Dim fileExtension As String    
Dim newFileName As String    
Dim fileSuffix As Integer    
Dim fullFilePath As String      

' Set the worksheet you want to save    
Set ws = ThisWorkbook.ActiveSheet      

' Set the file path (current workbook path)    
 
filePath = "/Users/xxxx/Desktop/Testing Save As.xlsm" & "\"      

' Set the base file name (current workbook name without extension)    
fileName = Left(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, ".") - 1)      

' Set file extension to .csv    
fileExtension = ".csv"      

' Initialize suffix counter    
fileSuffix = 1      

' Loop to check if a file with the same name and suffix already exists    
Do        
' Construct new file name with sequence number suffix        
newFileName = fileName & "_" & fileSuffix & fileExtension        
fullFilePath = filePath & newFileName        

' Increment suffix if file already exists        
fileSuffix = fileSuffix + 1    
Loop While Dir(fullFilePath) <> ""      

' Save the active sheet as a CSV file with the new name    

ws.Copy    
ActiveWorkbook.SaveAs fileName:=fullFilePath, _    
FileFormat:=xlCSVUTF8, CreateBackup:=False    

' Close the CSV file after saving
ActiveWorkbook.Close False      

' Notify the user    
MsgBox "File saved as: " & fullFilePath, vbInformation End Sub

5 Replies

  • Due to sandboxing on Mac, you probably need to grant access to the folder where you want the CSV's to be saved before it will work. Please take a look at this article:
    Mac - Grant Access to Multiple files (Microsoft Help)

     

    When saving a sheet as CSV, I like to first make a copy of the sheet in a new book so I don't need to worry about any unwanted changes happening to the source file.  To do that, I use the following lines:

     

    'Copy the active sheet to a new workbook.
    ActiveWorkbook.ActiveSheet.Copy

    Set newsheet = ActiveSheet

     

    'Convert all cells to values, if necessary
    newsheet.UsedRange.Value = newsheet.UsedRange.Value

     

    Then you can save the 'newsheet' workbook and close it without anything happening to the source file.

  • lnmlilo's avatar
    lnmlilo
    Copper Contributor

    Thank you for your response HansVogelaar 

     

    Removing the .xlsm from the file path - when I change it to filePath = "/Users/xxxx/Desktop/Testing Save As" & "/" I get this error message: 

    When I change it to  filePath = "/Users/xxxx/Desktop/Testing Save As" & "\" I get this error message:

     

  • Since you're on Mac, I think the line

    filePath = "/Users/xxxx/Desktop/Testing Save As.xlsm" & "\"

    should be

    filePath = "/Users/xxxx/Desktop/Testing Save As.xlsm" & "/"

    But do you really have an extension .xlsm in the file path?

Resources