Is it possible to choose a macro based upon a valve of a variable??

  • Thread starter Thread starter Rob947
  • Start date Start date
R

Rob947

I have written 6 macros and want to choose to use 1 of them depending upon
the value of “n” in cell A1

If “n” = 1 through 20 use Macro 1
If “n” = 21 through 40 use Macro 2
If “n” = 41 through 60 use Macro 3
If “n” = 61 through 80 use Macro 4
If “n” = 81 through 100 use Macro 5
If “n” = >100 use Macro 6

Is this possible??
 
use a case statement

Select case n
case 1 to 20
macro1
case 21 to 40
macro2


End Select
 
You practically have the code written already.

Sub RunMacro()
If n <= 20 Then Macro1
If n >= 21 And n <= 40 Then Macro2
If n >= 41 And n <= 60 Then Macro3
If n >= 61 And n <= 80 Then Macro4
If n >= 81 And n <= 100 Then Macro 5
If n >= 101 Then Macro6
End Sub

If you expect non integer values for n then you will want to replace
either the less than or equal to, or the greater than or equal to in
each If statement to simply less than, or greater than (depending on
how you want the variable treated).

HTH
 
Back
Top