Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default VB Code for summing control check boxes

HI

I have a sheet with 4 tick box controls on it, i would like a peice of vb
code that would add them up if they have a check in them. I have no idea
where to start.

Thanks in advance for the help.

Leigh
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default VB Code for summing control check boxes

Hi,

Right click yoor sheet tab, view code and paste this in and run it.


Sub Checked()
Dim Obj As OLEObject
For Each Obj In ActiveSheet.OLEObjects
If TypeOf Obj.Object Is MSForms.CheckBox Then
If Obj.Object.Value = True Then Count = Count + 1
End If
Next
Set Obj = Nothing
MsgBox Count
End Sub

Mike

"Leigh Douglass" wrote:

HI

I have a sheet with 4 tick box controls on it, i would like a peice of vb
code that would add them up if they have a check in them. I have no idea
where to start.

Thanks in advance for the help.

Leigh

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default VB Code for summing control check boxes

Preumably when you say 'add them up' you mean count how many are checked or
encode them in someway.

I also assume they are ActiveX controls rather than Form controls?

An example option below, for each control the event click is set up, which
calls a common process to evaluate their status, place code behind the
relevant worksheet where the controls are located.

Private Sub CheckBox1_Click()
GetResult
End Sub

Private Sub CheckBox2_Click()
GetResult
End Sub

Private Sub CheckBox3_Click()
GetResult
End Sub

Private Sub CheckBox4_Click()
GetResult
End Sub

Private Sub GetResult()
' counter
MsgBox -1 * (CheckBox1 + CheckBox2 + CheckBox3 + CheckBox4)
' binary encoded
MsgBox -1 * (CheckBox1 + CheckBox2 * 2 + CheckBox3 * 4 + CheckBox4 * 8)
End Sub

--

Regards,
Nigel




"Leigh Douglass" wrote in message
...
HI

I have a sheet with 4 tick box controls on it, i would like a peice of vb
code that would add them up if they have a check in them. I have no idea
where to start.

Thanks in advance for the help.

Leigh


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default VB Code for summing control check boxes

Nigel

Thanks very much for that peice of code, it works a treat. Any chance you
could tell me how i get the result to display in a given cell on the same
sheet as the check boxes?

Regards
Leigh

"Nigel" wrote:

Preumably when you say 'add them up' you mean count how many are checked or
encode them in someway.

I also assume they are ActiveX controls rather than Form controls?

An example option below, for each control the event click is set up, which
calls a common process to evaluate their status, place code behind the
relevant worksheet where the controls are located.

Private Sub CheckBox1_Click()
GetResult
End Sub

Private Sub CheckBox2_Click()
GetResult
End Sub

Private Sub CheckBox3_Click()
GetResult
End Sub

Private Sub CheckBox4_Click()
GetResult
End Sub

Private Sub GetResult()
' counter
MsgBox -1 * (CheckBox1 + CheckBox2 + CheckBox3 + CheckBox4)
' binary encoded
MsgBox -1 * (CheckBox1 + CheckBox2 * 2 + CheckBox3 * 4 + CheckBox4 * 8)
End Sub

--

Regards,
Nigel




"Leigh Douglass" wrote in message
...
HI

I have a sheet with 4 tick box controls on it, i would like a peice of vb
code that would add them up if they have a check in them. I have no idea
where to start.

Thanks in advance for the help.

Leigh



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default VB Code for summing control check boxes

I assume the checkbox names are checkbox1, checkbox2 and so on..Launch VBE
using Alt+F11. Insert a module and paste the below code..

In worksheet the below formula will return the value
=GetCheckBoxValue()

Function GetCheckBoxValue() As Long
For intTemp = 1 To 4
If ActiveSheet.OLEObjects("CheckBox" & intTemp).Object.Value = True Then
GetCheckBoxValue = GetCheckBoxValue + 1
End If
Next
End Function
--
If this post helps click Yes
---------------
Jacob Skaria


"Leigh Douglass" wrote:

HI

I have a sheet with 4 tick box controls on it, i would like a peice of vb
code that would add them up if they have a check in them. I have no idea
where to start.

Thanks in advance for the help.

Leigh



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default VB Code for summing control check boxes

Allocate the value to the range rather than display is a MsgBox

For example

Range("A10") = -1 * (CheckBox1 + CheckBox2 + CheckBox3 + CheckBox4)


--

Regards,
Nigel




"Leigh Douglass" wrote in message
...
Nigel

Thanks very much for that peice of code, it works a treat. Any chance you
could tell me how i get the result to display in a given cell on the same
sheet as the check boxes?

Regards
Leigh

"Nigel" wrote:

Preumably when you say 'add them up' you mean count how many are checked
or
encode them in someway.

I also assume they are ActiveX controls rather than Form controls?

An example option below, for each control the event click is set up,
which
calls a common process to evaluate their status, place code behind the
relevant worksheet where the controls are located.

Private Sub CheckBox1_Click()
GetResult
End Sub

Private Sub CheckBox2_Click()
GetResult
End Sub

Private Sub CheckBox3_Click()
GetResult
End Sub

Private Sub CheckBox4_Click()
GetResult
End Sub

Private Sub GetResult()
' counter
MsgBox -1 * (CheckBox1 + CheckBox2 + CheckBox3 + CheckBox4)
' binary encoded
MsgBox -1 * (CheckBox1 + CheckBox2 * 2 + CheckBox3 * 4 + CheckBox4 * 8)
End Sub

--

Regards,
Nigel




"Leigh Douglass" wrote in
message
...
HI

I have a sheet with 4 tick box controls on it, i would like a peice of
vb
code that would add them up if they have a check in them. I have no
idea
where to start.

Thanks in advance for the help.

Leigh




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Display of control check boxes and option buttons BRG Excel Programming 2 April 22nd 08 01:55 PM
Format Control on large number of check boxes Daesthai Excel Worksheet Functions 0 June 6th 07 04:09 AM
format control for large number of check boxes Daesthai Excel Worksheet Functions 0 June 5th 07 09:51 PM
check boxes and VBA code Paul Excel Programming 2 March 10th 05 09:13 PM
Code for Many check boxes Bob Excel Programming 0 January 23rd 04 02:16 AM


All times are GMT +1. The time now is 06:49 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"