Run a macro on save sheet

speedbit

New Member
Joined
Mar 2, 2021
Messages
3
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

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
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
 
Upvote 0
Solution
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,
 
Upvote 0
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

Hi! Here is me again, fightning with this macro! hehehe...

I want to modify it to get just a specific range of columns... let's say "A:F", "H" and "Z:AV"
Could you help me to get the right lines to edit?

Thanksin advance! =)
 
Upvote 0

Forum statistics

Threads
1,214,818
Messages
6,121,725
Members
449,049
Latest member
MiguekHeka

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
Back
Top