Create Error Message and Exit Macro if either of the conditions are not met

mwlenzac

New Member
Joined
Feb 20, 2019
Messages
4
I have an excel workbook and it regularly errors out on people when 1 of two conditions are not met when a macro is run, so I would like to actually check it as part of the macro rather than getting an excel error.

One is if "Sheet 10" is not visible. This usually indicates that there is no data in that sheet, but regardless the macro selects that sheet so if it is not visible, I would prefer it to shoot out a message saying, the sheet is not visible, please ensure you have followed step 1.

The other thing that can cause it to fail is if they have not entered any data.
"Sheet 10" cell B1 can be used to determine if data has been inputted. If that cell is blank, it should also shoot out a message stating that there is no valid user data, please follow step one and enter user data.

In both instances I would like it to then break out of the macro as it will fail otherwise
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Welcome to the Board!

Take a look at error handling in VBA:
http://www.cpearson.com/excel/errorhandling.htm
https://excelmacromastery.com/vba-error-handling/

Every error has an error number associated with it. You can see this when the error occurs.

Here is a simply example, where I am trying to select Sheet3, which may be hidden:
Code:
Sub Test()

'   Tell it to go to error handling routine if error found in code below this line
    On Error GoTo err_check
    
'   Actions to perform
    Sheets("Sheet3").Select
    
'   Reset default error handling
    On Error GoTo 0
    
'   Exit code so if no errors are found, it does not go to error handling
    Exit Sub
    
    
'Error handling block, typiccally placed at bottom of code
err_check:
    If Err.Number = 1004 Then
        MsgBox "Sheet3 is hidden, you cannot select it!"
    Else
        MsgBox Err.Number & Err.Description
    End If
    
End Sub
Take a look at those links, my example, and see if you can do what you want. If you run into issues, post your code back here and we can help you clean it up.
 
Last edited:
Upvote 0
Hi Joe
Thanks for the welcome.
I will take a look at the links and example and see how I go, I think it should cover me
Cheers
 
Upvote 0
You are welcome!

Glad I was able to help. :)
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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