Run a macro on save sheet

speedbit

New Member
Joined
Mar 2, 2021
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Note: I'm too basic in VBA/macros...

I have a macro that exports my sheet as CSV without leaving the XLSM file, that works great:

VBA Code:
Sub ExportCSV()
    Dim MyFileName As String
    Dim CurrentWB As Workbook, TempWB As Workbook

    Set CurrentWB = ActiveWorkbook
    ActiveWorkbook.ActiveSheet.UsedRange.Copy

    Set TempWB = Application.Workbooks.Add(1)
    With TempWB.Sheets(1).Range("A1")
        .PasteSpecial xlPasteValues
        .PasteSpecial xlPasteFormats
    End With

    MyFileName = CurrentWB.Path & "\" & Left(CurrentWB.Name, InStrRev(CurrentWB.Name, ".") - 1) & ".csv"
    'Optionally, comment previous line and uncomment next one to save as the current sheet name
    'MyFileName = CurrentWB.Path & "\" & CurrentWB.ActiveSheet.Name & ".csv"


    Application.DisplayAlerts = False
    TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSVUTF8, CreateBackup:=False, Local:=False
    TempWB.Close SaveChanges:=False
    Application.DisplayAlerts = True
End Sub

I just need to run it automatically each time I save my sheet...
It´s possible?? How to do it?

Thanks in advance! =)
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,691
Welcome to the board!

Have your VBA Editor open and double click the ThisWorkbook of your file. Make sure you change the "General" to "Workbook" in the left dropdown of your editor. Then select the "Before save" from the left drop down. Now, if you've saved the "ExportCSV" macro saved in your normal module you just call it:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Call ExportCSV

End Sub
 
Solution

speedbit

New Member
Joined
Mar 2, 2021
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Welcome to the board!

Have your VBA Editor open and double click the ThisWorkbook of your file. Make sure you change the "General" to "Workbook" in the left dropdown of your editor. Then select the "Before save" from the left drop down. Now, if you've saved the "ExportCSV" macro saved in your normal module you just call it:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Call ExportCSV

End Sub

Just PERFECT!! Solution: accurate. Explanation: BRILLIANT! =D

Thank you very much, for your kindly help and attention.
BR,
 

Watch MrExcel Video

Forum statistics

Threads
1,129,804
Messages
5,638,455
Members
417,025
Latest member
MusterDuster

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top