473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Simple CheckBox Question

Hi -- I'm working on some VBA in Excel 2000 to automate some spreadsheet tasks. I have a really basic question about checkboxes... I've been using a "toy" program to try to figure out how to get the program to tell which box is checked. The form is created under forms in the VBA IDE (not in the sheet itself)

Expand|Select|Wrap|Line Numbers
  1. Sub Macro1()
  2.  
  3.  
  4. Dim count As Integer
  5. count = 0
  6. MyForm.Show
  7.  
  8.     If MyForm.CheckBox1.Value Then
  9.         count = count + 1
  10.  
  11.     End If
  12.  
  13.     If MyForm.CheckBox2.Value = 1 Then
  14.         count = count + 1
  15.  
  16.     End If
  17.  
  18.     If MyForm.CheckBox3.Value = vbChecked Then
  19.         count = count + 1
  20.  
  21.     End If
  22.  
  23.     MsgBox (count)
  24.  
  25. End Sub
  26.  
  27.  
Whenever I run this I get count = 1 no matter what is checked

I have also tried approaches "viewing the code" by right clicking on the control and attempting similar code (except using CheckBox1.Value instead.

If the CheckBox1_Click() is the only way to do it, how can I reference the responses in another macro (like with a global variable, perhaps?)

Apologies for the noob question, but I have searched around for a basic explanation of Checkboxes with a "Hello World" type example and found none.
Jan 23 '08 #1
6 1998
kadghar
1,295 Expert 1GB
(...)
Whenever I run this I get count = 1 no matter what is checked

I have also tried approaches "viewing the code" by right clicking on the control and attempting similar code (except using CheckBox1.Value instead.

If the CheckBox1_Click() is the only way to do it, how can I reference the responses in another macro (like with a global variable, perhaps?)

Apologies for the noob question, but I have searched around for a basic explanation of Checkboxes with a "Hello World" type example and found none.
This is because when you Show the Form, the Macro in the module 'stops running' until you close MyForm.

When you close it, all checkboxes values are set to false. So the first IF wont add anything to Count, the second wont do it as well, but the third will, since vbChecked is not recognized by VBA so its taken as "empty" or in this very case as "False".. this is why the 3rd IF adds 1 to Count.

HTH
Jan 23 '08 #2
Expand|Select|Wrap|Line Numbers
  1. ' Macro
  2.  
  3. Sub Macro1()
  4.    MyForm.Show
  5. End Sub
  6.  
Expand|Select|Wrap|Line Numbers
  1. ' UserForm :  MyForm
  2. ' Checkbox :  Checkbox1
  3. ' Checkbox :  Checkbox2
  4. ' Checkbox :  Checkbox3
  5. ' Button   :  CommandButton1
  6.  
  7. Private Sub CommandButton1_Click()
  8.    Dim count As Integer
  9.  
  10.    count = 0
  11.  
  12.    If MyForm.CheckBox1.Value Then
  13.       count = 1
  14.       MsgBox count & " is checked."
  15.    End If
  16.  
  17.    If MyForm.CheckBox2.Value = True Then
  18.       count = 2
  19.       MsgBox count & " is checked."
  20.    End If
  21.  
  22.    If MyForm.CheckBox3.Value = True Then
  23.       count = 3
  24.       MsgBox count & " is checked."
  25.    End If
  26. End Sub
  27.  
Jan 23 '08 #3
kadghar
1,295 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. (...)   
  2.    count = 0
  3.  
  4.    If MyForm.CheckBox1.Value Then
  5.       count = 1
  6.       MsgBox count & " is checked."
  7.    End If
  8.  
  9.    If MyForm.CheckBox2.Value = True Then
  10.       count = 2
  11.       MsgBox count & " is checked."
  12.    End If
  13.  
  14.    If MyForm.CheckBox3.Value = True Then
  15.       count = 3
  16.       MsgBox count & " is checked."
  17.    End If
  18. End Sub
  19.  
I dont think this will count how many checkboxes are checked,
Count=count+1 was all right, no need to change it.

By the way, I think it's a good idea to use a Sub to show the form, and then, you can use another sub to count the checkboxex (this sub can be in the same module and just CALL it when you need it, or in a command's event in the Form, as WinblowsMe suggests)
Jan 23 '08 #4
Ok very cool.

I hadn't realized that the macro was suspended when the form was active.

Now in terms of returning count to the main macro, I've tried declaring it as global (as Variant or as Integer) and it seems to still be outside of the scope of the macro. What is the best way to return this?

I'm not that new to programming, but I just can't seem to "get" VB lol

Thanks very much for help on the prior post and for any help on this one!!
Jan 23 '08 #5
kadghar
1,295 Expert 1GB
Ok very cool.

I hadn't realized that the macro was suspended when the form was active.

Now in terms of returning count to the main macro, I've tried declaring it as global (as Variant or as Integer) and it seems to still be outside of the scope of the macro. What is the best way to return this?

I'm not that new to programming, but I just can't seem to "get" VB lol

Thanks very much for help on the prior post and for any help on this one!!
yes, thats a good idea

what i recommend you is to do this

public Count as integer '(make it global)

in macro1:
show the form
msgbox Count

in terminate event of the form:
do the 'count' thing

HTH
Jan 23 '08 #6
kadghar
1,295 Expert 1GB
oh, i forgot
I will also recommend you to use

Expand|Select|Wrap|Line Numbers
  1.     If MyForm.CheckBox1.Value Then
  2.     count = count + 1        
  3. End If
this way all times you're working with checkboxes.
Jan 23 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: claudel | last post by:
Hi I have a newb PHP/Javascript question regarding checkbox processing I'm not sure which area it falls into so I crossposted to comp.lang.php and comp.lang.javascript. I'm trying to...
3
by: MarkVan | last post by:
I am pretty new to ASP. HTML Page <input type="checkbox" name="chkStat" value="" checked>Low ASP Page chkCheck = request.form("chkStat") response.write chkCheck I get nothing? How do I...
2
by: Zhang Weiwu | last post by:
Hello. Very simple question. Here is my HTMl and I wish each <input type="text"> To position that they look like aligned to right, while the other part (the checkbox and its label) look like...
10
by: Jennyfer J Barco | last post by:
Hello, I have a datagrid that brings some information from a query. I need to have a checkbox in each row so the user can select the rows he wants to reprint. Is it possible to have a checkbox...
2
by: Allain Bøge | last post by:
It is really a simple question. Visual Basic .NET (2003) I create 2 forms (Form1 and Form2) I create a checkbox in Form1 (checkbox1) I create a checkbox in Form2 (checkbox1) I go to Form1...
5
by: W.Sh | last post by:
Hello Everyone! I'm having some issues with javascript that I can't seem to resolve... Basically, I have a very simple code that's supposed to change the innerHTML of a span element whenever I...
34
by: clinttoris | last post by:
Hello Experts, I have been told to post this in the Javascript forum as I want to do this client side just before my form gets submitted. Once the user clicks the submit button a javascript...
2
by: SoulScriber | last post by:
This comes to be an IE vs. Firefox issue. What I'm trying to do is 'onchange' of a checkbox have a table display underneath. Firefox works fine (when you click on the 'Check Here if your...
1
by: rjoseph | last post by:
HI Guys I have a simple check box form (using GET as the method) with a list of 10 different UK counties. I have validated the form so that users have to select no more and no less than 5 UK...
4
by: ameshkin | last post by:
I have a checkbox with an ID of svc_tp_1, and an image that corresponds with this checkbox below it. <input type="checkbox" name="checkbox" id="svc_tp_1" value="svc_tp_1" / <img...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.