Create an error message when a condition is not met.

slavoy

New Member
Joined
Nov 14, 2007
Messages
35
I somehow want to make an error if a certain condition is not met and have a message box pop up to explain the error and then end my macro, but continue if there is no error.

The condition is this: if the number (value) in a particular cell is not equal to the total number of sheets in my workbook then I want the macro to end and display the message: "You need to Press Ctrl+Shift+C."

The value of the particular cell can be found by the following statement:

Sheets("Initial Input").Range("O10").Value

and the total number of sheets should be able to be found with something like this:

Application.Sheets.Count

I was thinking about using an if statement, but I'm open to using any kind of idea.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try this.
Code:
If Sheets("Initial Input).Range("O10").Value <> ThisWorkbook.Sheets.Count
     Msgbox "You need to Press Ctrl+Shift+C."
     Exit Sub
End If
 
' continue with rest of code
 
Upvote 0
Ok thanks. This worked just the way I wanted it to except I had to add a "then" to make the if statement work and a " after Initial Input. I tried something like this before my first post but I had some of my syntax wrong. This cleared it up for me.

Thanks again.
 
Upvote 0
Oops.:oops:

Just a couple of typos, glad you sorted it yourself.:)
 
Upvote 0

Forum statistics

Threads
1,213,533
Messages
6,114,179
Members
448,554
Latest member
Gleisner2

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