|
|
| Back to Excel Homepage | Excel VBA - Reference Guide |
|
Menu Making decisions (Case)
Other links
|
This is one of the 'control flows' VBA provides which is required to make your VBA code procedures more flexible, reduce the amount of code required and make the system think for itself! Without any of these 'control flows', your code is very linear and rigid which never is really suitable when trying to mimic 'real world' processes.
The four 'control flows' are: 2. Select Case...End Select
Select Case statementThis is an alternative way to write logical statements and designed for multiple and similar conditions tested in a look up table together.
It is deemed faster than a conventional If statement and more clinical to write and understand.
Structure (syntax): Select Case Grade/Expression
Case Value 1 Execute multiple lines of code if true 1 .....
Case Value 2 Execute multiple lines of code if true 2 .....
Case Value 3 Execute multiple lines of code if true 3 .....
Case Value N Execute multiple lines of code if true N .....
Case Else
Execute multiple lines of code if false .....
End Select
Case Value is the value being test logically against the Grade or Expression for a true match. It continues down the list in order until it finds a true match with a catch using Case Else as the false option.
It sits all within the Select Case block using the End Select to terminate the logical test environment.
Note the keyword To meaning a range (the And operator) which is much easier to write as a condition. Also, using the , (comma) which acts as the Or operator only enhances the way you can use this statement quickly.
|
VBA Keywords: ActiveCell, Font, Bold, Italic, Underline, Color, ClearFormats, IsNumeric and Exit Sub. |
|
© copyright 2010 TP Development & Consultancy Ltd, All Rights Reserved. All trademarks are copyrighted by their respective owners. Please read our terms of use and privacy policy. |