Returning to last used sheet

CV899000

Board Regular
Joined
Feb 11, 2016
Messages
98
Hi,

I have this code that prompts a message if a user tries to add a new sheet in a workbook.

I have three sheets that is allowed to be in the workbook called "Information", "Prices", "Copied Prices" in that order.

If a user is working in sheet "Prices" and decides to try and add a new sheet, the message promts saying he is not allowed to do so, but he then ends up at "Copied Prices" sheet because that is the next sheet in line, but not necessarily the last sheet that he was using.

How do I make excel return to the last active sheet?

Please see the code below:

Private Sub Workbook_NewSheet(ByVal Sh As Object)
'Update 20140623
With Application
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sh.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End With
MsgBox "You are not allowed to add a new sheet"
End Sub
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this all in ThisWorkbook module
Code:
Dim LastOn As String

Private Sub Workbook_NewSheet(ByVal Sh As Object)
'Update 20140623
With Application
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Sheets(LastOn).Activate
Sh.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End With
MsgBox "You are not allowed to add a new sheet"
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
LastOn = ActiveSheet.Name
End Sub
 
Upvote 0
Hello,

try

Code:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
'Update 20140623
With Application
Application.ScreenUpdating = False
Application.DisplayAlerts = False
If ActiveSheet.Index <> ActiveWorkbook.Sheets.Count Then
    MY_SHEET = ActiveSheet.Index
    Sheets(MY_SHEET - 1).Activate
End If
Sh.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End With
MsgBox "You are not allowed to add a new sheet"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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