Thread: Setup Userform
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Harald Staff Harald Staff is offline
external usenet poster
 
Posts: 1,327
Default Setup Userform

Hi Martin

Whenever I deal with multiple boolean values, I usually cram them together
into a Long number for storage, initialization or analysis. The idea is tha
number 9 is binary number 1001 -where each 1 or 0 represents true or false.
32-bit Windows' Long can handle 31 boolean values; 31 checkboxes or similar.

Try 4 checkboxes on a userform and this code to a button:

Private Sub CommandButton1_Click()
Dim L As Long 'our number
Dim C As Long 'a counter only
L = Abs(CheckBox1.Value + _
2 * CheckBox2.Value + _
4 * CheckBox3.Value + _
8 * CheckBox4.Value)
MsgBox "Result can be stored as " & L & chr(10) & _
"You can do a Select Case decision here. Or something like:"
For C = 1 To 4
MsgBox C & " is checked " & IsChosen(L, C)
Next
End Sub

Function IsChosen(L As Long, Pos As Long) As Boolean
IsChosen = ((2 ^ (Pos - 1)) And L)
End Function

--
HTH. Best wishes Harald
Followup to newsgroup only please

"Martin Los" skrev i melding
...
I have a Userform with 2 comboboxes (date and month), 4
checkboxes and 4 textboxes. Depending on the selection of
the 4 checkboxes I want to the 4 textboxes to represent
text (or not).

In total there are 14 posibilities of what needs to be
shown. How can I program with a VBA routine whether the
textboxes need to show a certain text, depending on
whether the checkboxes are true or false?

I want to prevent loads of if .. then structures like:
"If checkbox1 = true then
If checkbox2 = true then
If checkbox3 = true then
....
textBox1.Text = "Text1"
Else......"

1) Are boolean variables to check state of checkboxes
easier to use to get textboxes to represent text (or not)?

2) How can I get default text of comboboxes to be shown in
Textbox? If I have on Userform_Initialize():

combobox1.Value = Format(Ayer, "d")
combobox2.Value = Format(Ayer, "mmm")

and
textbox1.Text = combobox1.Value

Then on showing the userform, only the combobox values are
shown with dates, but not the textboxes.

Thanks in advance

Martin


Yours sincerely

Martin