CheckBox in Excel VBA


I have four Checkboxes in my UserForm, if the user selects more than one option, than it should display in Msgbox the values selected and also, if the user does not select any than an message should be displayed "You should select any one value"
srikanthv2322Asked:
Who is Participating?
 
DaveCommented:
Each of the ChechBoxes is examined as a Boolean test to see if it was checked

strMsg is a result string that totals the responses.

Initially it is blank
If the first test is true then
If Me.CheckBox1 Then strMsg = strMsg & "You checked 1" & vbNewLine
"You Checked 1" is stored in strMsg
If CheckBox2 is true then "You Checked 2" is stored in strMsg

so if just 1 was true
strMsg = "You checked 1"

if just 2 was true
strMsg = "You checked 2"

if 1 and 2 are both true
strMsg =
"You checked 1"
"You Checked 2

etc

At the end if no boxes were check strMsg is empty, ie
Len(StrMsg) =0
is true, and the pls entrer message pops up

cheers

Dave



0
 
DaveCommented:
hth

Dave
Private Sub CommandButton1_Click()
    Dim strMsg As String
    If Me.CheckBox1 Then strMsg = strMsg & "You checked 1" & vbNewLine
    If Me.CheckBox2 Then strMsg = strMsg & "You checked 2 & vbNewLine"
    If Me.CheckBox3 Then strMsg = strMsg & "You checked 3" & vbNewLine
    If Me.CheckBox4 Then strMsg = strMsg & "You checked 4" & vbNewLine
    If Len(strMsg) = 0 Then
        MsgBox "You should select any one value!'"
    Else
        MsgBox strMsg
    End If
End Sub

Open in new window

cb.xlsm
0
 
DaveCommented:
Note this code allows for one or more boxes to be checked - which I presume is your intent. If so it messages each selected checkbox

Cheers

Dave
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
yjchong514Commented:
Similiar example:
Private chkArray(3) as Checkbox

_Load event
Set chkArray(0) = chkFirst
Set chkArray(1) = chkSecond
...

in the _Unload event
Dim intLoop as Integer
  For intLoop = 0 to 3
    Set chkArray(intLoop) = Nothing
  Next intLoop
...

Dim intLoop as Integer
Dim intCount as Integer
  intCount = 0
  For intLoop = 0 to 31
    If chkArray(intLoop).Value = vbChecked Then  
      intCount = intCount + 1
    End If
  Next intLoop

  MsgBox "Selected: " & intCount
0
 
srikanthv2322Author Commented:
Its working, just want to know ,What is that Strmsg and how did you arrive at the solution

Can you make it clear with understanding, i will give u full points
0
 
TracyVBA DeveloperCommented:
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.