Solved

CheckBox in Excel VBA

Posted on 2011-02-26
7
1,039 Views
Last Modified: 2012-05-11

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"
0
Comment
Question by:srikanthv2322
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34990321
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
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34990326
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
 
LVL 6

Expert Comment

by:yjchong514
ID: 34990329
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:srikanthv2322
ID: 34990340
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
 
LVL 50

Accepted Solution

by:
Dave Brett earned 500 total points
ID: 34990354
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
 
LVL 24

Expert Comment

by:broomee9
ID: 35225365
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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question