Hi Daz, thanks for the pm.
This will probably need some editing at your end so I will walk you through the code.
First we set up the folder to process, REMEMBER the trailing backslash, and set the file variable to the first XLSM file in that folder.
Rich (BB code):
sPath = "C:\temp\" 'REMEMBER TRAILING BACKSLASH
sFile = Dir(sPath & "*.xlsm")
You will get an alert when saving as a csv file, i.e., only one sheet, lose Excel functionality, etc; so we will disable alerts. An error trap will ensure the application settings are reset before the code ends.
Rich (BB code):
'disable saving CSV alerts - use error trap to ensure application settings are reset
On Error GoTo errHandler
Application.DisplayAlerts = False
We loop through the files until no more XLSM files are found:
Rich (BB code):
'loop through the folder
Do Until sFile = ""
Set the source workbook variable, open the SOURCE wourbook and build up the new file name:
Rich (BB code):
'open the workbook and build the new file name
Set wbSource = Workbooks.Open(sPath & sFile)
newFileName = sPath & Replace(wbSource.Name, "xlsm", "") & ".csv"
ASSUME the sheet to be saved as csv is the
first worksheet - EDIT if necessary.
Copy this sheet into a new target workbook.
Save the target workbook as a csv file.
Close source and target files.
Rich (BB code):
'ASSUME sheet to be saved as csv is first worksheet
wbSource.Worksheets(1).Copy
Set wbTarget = ActiveWorkbook
wbTarget.SaveAs Filename:=newFileName, FileFormat:=xlCSV
wbTarget.Close SaveChanges:=False
wbSource.Close SaveChanges:=False
Clear the variables from memory and get the next file.
Rich (BB code):
'get next fileThis will probably need some editing at your end
Set wbSource = Nothing
Set wbTarget = Nothing
sFile = Dir()
Loop
And end of with our error trap ensuring the Application setting we disabled are reset.
Rich (BB code):
errHandler:
Application.DisplayAlerts = True
End Sub
Putting it all together, place the code in a standard module, i.e., Insert=>Module.
Rich (BB code):
Option Explicit
Sub SaveAsCsv()
Dim sPath As String
Dim sFile As String
Dim wbSource As Workbook
Dim wbTarget As Workbook
Dim newFileName As String
sPath = "C:\temp\" 'REMEMBER TRAILING BACKSLASH
sFile = Dir(sPath & "*.xlsm")
'disable saving CSV alerts - use error trap to ensure application settings are reset
On Error GoTo errHandler
Application.DisplayAlerts = False
'loop through the folder
Do Until sFile = ""
'open the workbook and build the new file name
Set wbSource = Workbooks.Open(sPath & sFile)
newFileName = sPath & Replace(wbSource.Name, "xlsm", "") & ".csv"
'ASSUME sheet to be saved as csv is first worksheet
wbSource.Worksheets(1).Copy
Set wbTarget = ActiveWorkbook
wbTarget.SaveAs Filename:=newFileName, FileFormat:=xlCSV
wbTarget.Close SaveChanges:=False
wbSource.Close SaveChanges:=False
'get next fileThis will probably need some editing at your end
Set wbSource = Nothing
Set wbTarget = Nothing
sFile = Dir()
Loop
errHandler:
Application.DisplayAlerts = True
End Sub
Hope this helps,
Bertie