Solved

CheckBox in Excel VBA

Posted on 2011-02-26
7
1,035 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

832 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