Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CheckBox in Excel VBA

Posted on 2011-02-26
7
Medium Priority
?
1,041 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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:Tracy
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

604 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