nested ifs vba

AI Training: For Busy Decision Makers & Professionals Book now

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Nested Ifs in VBA

Nested Ifs in VBA

resolvedResolved · Low Priority · Version 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:

Create your own custom list on Excel 2010!

If you know how to use the auto-fill option on Excel then why not create your own customs lists?

The auto fill feature saves you time by allowing you to enter one of the list entries into a cell and then use your mouse to automatically drag the rest of the list into the cells below, above or to either side of the initial cell. When using your mouse to perform this task you will see a thin black cross appear at the bottom right hand side of the cell. Click, hold and drag to make the list appear.

Default lists include weekdays and months. To create your own list in Excel 2010 do the following;

>File
>Options
>Advanced
>Scroll right to the bottom of the page and you will see a buttom "edit custom lists", click this button
>enter your list in the list entries
>click add

Now try it out. Good luck.
>

View all Excel hints and tips


Server loaded in 0.14 secs.