I want the procedure below embedded in a macro-enabled Excel (xlsm) file.
Sub SaveWorksheetsAsCSV()
Dim WS As Excel.Worksheet
Dim CurrentWorkbook As String
Dim CurrentFormat As Long
CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
Application.DisplayAlerts = False
For Each WS In ThisWorkbook.Worksheets
WS.Copy
ActiveWorkbook.SaveAs Filename:=CurrentWorkbook & "-" & WS.Name & ".csv", FileFormat:=xlCSV
ActiveWorkbook.Close savechanges:=False
ThisWorkbook.Activate
Next
Application.DisplayAlerts = True
End Sub
I want the procedure to be assigned to a hotkey (Shift+Alt+S), so I also embed this procedure.
Private Sub Workbook_Open()
Application.OnKey "+%s", "SaveWorksheetsAsCSV"
End Sub
I put both of these procedures in the “ThisWorkbook” of the “Microsoft Excel Objects” folder, because I want the hotkey’ed save as CSV procedure to be usable from any sheet after the file is opened.
I can bring up the macro list with Alt+F8 and run ThisWorkbook.SaveWorksheetsAsCSV and all sheets are saved as CSV files as expected. But when I use the hotkey (Shift+Alt+S), I get the error message “Cannot run macro...” I'm fairly certain it’s not a permission thing, for testing I’ve turn on “Enable all macros” in Macro Security settings.
Can someone help me set this up properly? This XLSM file will be used as a template that people will copy and modify and then convert to CSV for import into another tool.