Close workbook if sheet does not exist

bptaw

Board Regular
Joined
Feb 27, 2017
Messages
69
Office Version
  1. 2016
Platform
  1. Windows
I have set up a workbook with a password that expires after a set date. After that date all bar one worksheets are also deleted. I want the workbook to close if a specific sheet cannot be found (I get vba errors as it looks for the deleted sheets) , or otherwise proceed to show the login.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Maybe try on a test workbook the below as an example...

VBA Code:
Sub Saveit()
    Dim sht1 As String
    sht1 = "Sheet4" ' change sheet name to suit
   
    If Not Evaluate("isref('" & sht1 & "'!A1)") Then
        ThisWorkbook.Close False
    Else
        MsgBox "The sheet does exist"  ' whatever else you want goes here
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,105
Messages
6,123,118
Members
449,096
Latest member
provoking

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