Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Nested Ifs in VBA
Nested Ifs in VBA
Resolved · 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?
For upcoming training course dates see: Pricing & availability
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
|

