Combining Subs in VBA

DrPancho

Board Regular
Joined
Jun 12, 2015
Messages
80
Hi

Sorry if my terminology isn’t correct I’m quite new to this, I’m stuck trying to get the below 2 actions to run one after another.

When a user attempts to save, a msgbox will appear saying “Nope no saving I’m afraid!” with only 1 option to cancel.

When the user click cancel I want the workbook to change from the active sheet to another sheet in the same workbook.

Can this be done?

Below is the code I’ve been messing around with but as I said I’m very new and pretty stuck lol.

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    MsgBox "Nope no saving I'm afraid!", , "Your request has been denied!"
    Cancel = True
    
End Sub
Sub Activate_Workbook()
    'Activate Worksheet
    Workbooks("MyWorkbook.xls").Sheets("Change to this worksheet!").Activate
End Sub

Many thanks in advance.

Also the title might not be best suited for this request, If you can suggest something else it may help
somebody looking for something similar find it easier :)
 

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.
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  Cancel = True
  MsgBox "Prompt:=Nope no saving I'm afraid!", Title:="Your request has been denied!"
  Workbooks("MyWorkbook.xls").Sheets("Change to this worksheet!").Select
End Sub
But then you can never save the workbook ...
 
Upvote 0
I can, i can save it in "Design Mode"... nobody else can save the workbook.. Thats the plan anyway lol

Also, Thanks i will try that now :)
 
Upvote 0
@sgh - When i run the code i get the error "Run-time error "9": Subscript out of range"
Any ideas?

@deletedalien - Yes i was thinking that, Is there any way to remove that option from the menu upon opening the workbook?

Thanks peeps
 
Upvote 0
@sgh - When i run the code i get the error "Run-time error "9": Subscript out of range"
Any ideas?

@deletedalien - Yes i was thinking that, Is there any way to remove that option from the menu upon opening the workbook?

Thanks peeps


Disable Saveas:

Code:
Private Sub workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Dim lReply As Long
If SaveAsUI = True Then
lReply = MsgBox("Sorry, you are not allowed to save this Workbook."
End If
End Sub
 
Upvote 0
@sgh - When i run the code i get the error "Run-time error "9": Subscript out of range"
There has to be a workbook named MyWorkbook.xls open containing a sheet named Change to this worksheet!
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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