Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

nested ifs vba

ResolvedVersion 2003

Dimitrios has attended:
Excel Advanced course
Excel VBA Intro Intermediate course

Nested Ifs in VBA

I had created a piece of code trying to test some data adn I had to use numerous if statements. Peter suggested that using the Case function would be better and more effiecient. How exactly can I replace Ifs with Cases?

RE: Nested Ifs in VBA

Hi Dimitrios,

Thank you for your question.

You start with 'Select Case' and the name of the variable.

E.g. Select Case MarkUp

Multiple conditions are handled by 'Case Is'.

E.g. Case Is MarkUp > 2000

Underneath each 'Case Is' you type the true answer.

E.g. Case Is MarkUp > 2000
Commission = MarkUp * 0.05

Case Is MarkUp > 1000
Commission = MarkUp * 0.03

Continue this pattern until all the conditions have been entered.

Use 'Case Else' to input the answer for all the remaining values not covered by the conditions.

E.g. Case Else
Commission = 0

Finally you need to close the Select command with:

E.g. End Select

I hope this answers your question.

Regards

Simon

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Removing the Ribbon from view in Excel 2010

At times when you want to view the whole spreadsheet, try double clicking on the ''Home'' tab on the ribbon which will hide the ribbon from view.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.09 secs.