Thursday, December 17, 2009

Select Case VBA

Select Case is similar to if....Then .....Else ,except that you can choose from several condition values.

The general form is

Select Case expression
Case Value1
one or more VB statements for action1
Case Value2
one or more VB statements for action2
.....
.....
Case Valuem
one or more VB statements for actionm
[case else
one or more statements for action otherwise]
End Select

You can have as many case lines as you like and you can have more than one value on each line. You can even have a range of values by using To.


Example:

In the following example, the variable n takes as its value the content of the active cell. Control the passes to the corresponding Case statement.

Note the use of To in one of the case statements.

n = Activecell
select case n
case 12
statementA
case 24, 36
statementB
case 48 To 96
statementC
....
....
Case Else
statementD
end select

Else statement is a replacement of  if statement when we need to be right more if statement.....

No comments:

Subscribe Now: From your browser

Search:

Learn Office Excel