Solved

CheckBox in Excel VBA

Posted on 2011-02-26
7
1,034 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

708 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now