Save the document created with VBA

S Oberlander

Board Regular
Joined
Nov 25, 2020
Messages
147
Office Version
  1. 365
Platform
  1. Windows
My excel file is blocked from savings with a beforeclose even handler, however, to offer an alternative way to save I created the below sub.
It works great when I step thru it, but when I just click on the button linked to it, it tries to save the blocked workbook so nothing gets saved...
VBA Code:
Sub savecopy()

    Sheets("Pivot").Copy
    ActiveSheet.Shapes.Range(Array("Button 1")).Delete
    With Application.FileDialog(msoFileDialogSaveAs)
        .InitialFileName = ThisWorkbook.Path & "\"
        If .Show Then .Execute
    End With
    
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
You may need to temporarily disable events so that your save attempt does not trigger your BeforeSave event to fire.
See if this works:
Rich (BB code):
Sub savecopy()

    Sheets("Pivot").Copy
    ActiveSheet.Shapes.Range(Array("Button 1")).Delete
    Application.EnableEvents = False
    With Application.FileDialog(msoFileDialogSaveAs)
        .InitialFileName = ThisWorkbook.Path & "\"
        If .Show Then .Execute
    End With
    Application.EnableEvents = True
    
End Sub
 
Upvote 0
The things is, that when I step thru the save-as is applying to the newly created workbook, but when I click on the button the save-as is applying to the initial workbook.
So when I used Application.EnableEvents, it simply did a save-as on the blocked workbook.
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,715
Members
449,118
Latest member
MichealRed

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