Case Selection Example

  • Thread starter Thread starter toocold
  • Start date Start date
T

toocold

Hi everyone,

I think that using select case might be my best option fo
accomplishing what I want to but I am really not sure. What I have i
a series of checkboxes on a userform, that a user may select to achiev
a desired result. For example, checkbox1 and checkbox2. A user has
options, he/she may leave both blank, select checkbox1 and leav
checkbox2 blank, select checkbox2 and leave checkbox1 blank, or selec
both checkboxes.

Each of these would result in different code being ran by excel. Ca
anyone give me an example of how I would set this up and use "selec
case" in this instance. Or is there a better way to handle this. (I
my actual program I have six checkboxes)

Once again, thank you in advance for any advice. i
Cheers,
d
 
This worked for me (attached to a command button on the userform)

Private Sub CommandButton1_Click()
Dim x As Boolean
Dim y As Boolean

x = Me.CheckBox1.Value
y = Me.CheckBox2.Value

Select Case True
Case x And y
MsgBox "Case 1"
Case x And Not y
MsgBox "Case 2"
Case Not x And y
MsgBox "Case 3"
Case Not x And Not y
MsgBox "Case 4"
End Select

End Sub
 
Hello TooCold,

This macro is for 4 checkboxes. It converts the Checkbox value of True
(Checked) or False (Not checked) to a 1 or 0. Each checkbox value is
then multiplied by a power of 2 to create a bitmapped word of the
checkboxes states. If you want to run code for possible combination of
6 checkboxes you would have 64 Case statements.


Code:
--------------------
Sub SelectExample()

Dim ChkBox1
Dim ChkBox2

With UserForm1
ChkBox1 = Abs(.CheckBox1.Value)
ChkBox2 = Abs(.CheckBox2.Value) * 2
End With

ChkBoxes = ChkBox1 + ChkBox2

Select Case ChkBoxes
Case 0
'Code for 1 and 2 cleared
Case 1
'Code for 1 checked, 2 cleared
Case 2
'Code for 1 cleared, 2 checked
Case 3
'Code for 1 and 2 checked
End Select

End Sub
 
Back
Top