View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
George Nicholson George Nicholson is offline
external usenet poster
 
Posts: 149
Default verify state of option button

A couple of possible approaches:

If opt1 + opt2 + opt3 + opt4 Then
Msgbox "Something selected"
Else
' All are False (or Null)
Msgbox "Nothing selected"
End If

OR

Select Case True
Case opt1, opt2, opt3, opt4
' One of the above is True
Msgbox "Something selected"
Case Else
' All are False (or Null)
Msgbox "Nothing selected"
End Select

I like the 2nd appoach cause 1) its easier to read IMO, 2) easier to add
additional buttons 3) easier to break a button off to a seperate Case
statement. In my mind, it mimics the functionality of an Option group
(available with Access or vb control sets, etc.) fairly well.

If you are working on a UserForm (which has a Controls collection), you
could also do something like:

Dim i As Integer
Dim iSelected As Integer

For i = 1 To 4
If Controls("Opt" & i) Then
iSelected = i
Exit For
End If
Next i
If iSelected = 0 Then
MsgBox "None selected"
End If

HTH,

"stewart" wrote in message
oups.com...
I have a group of four option buttons on a userform. The user is
required to check select one of them. What can I put in my code to
verify this. What I currently have is below but it does not seem like
it is the most efficient way of completing this task as I have this
occurrence 34 separate times.


sub verify()
If opt1.Value = False And opt2.Value = False And opt3.Value = False
And opt4.Value = False Then
GoTo 2:
ElseIf opt5.Value = False And opt6.Value = False And opt7.Value =
False And opt8.Value = False Then
GoTo 2
end if

'other code below
...
exit sub
2: msgbox "Please verify that you have completed each section"
end sub