Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default smart & tidy code for many checkBox_Change()


Hi all,

I've got questions again I've got a userform with about 3
checkBoxes. If any of the checkBoxes is ticked or unticked, I'd lik
the same block of code to be executed. How can I write smart and tid
code instead of writing 30 checkBox1_Change() ? any idea? Thanks

All the best,
Fendi

--
Fendi
-----------------------------------------------------------------------
Fendic's Profile: http://www.excelforum.com/member.php...fo&userid=2395
View this thread: http://www.excelforum.com/showthread.php?threadid=39365

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default smart & tidy code for many checkBox_Change()


Hi Fendic,

that can be done by using a class module.


Insert a new CLASS MODULE. (not a normal module)
In the porperties window give it a unique name
(clsCombo in this example)

copy the code.


''' Code for ClassModule Named clsCombo
Option Explicit
'the trick is in the withevents keyword.
Public WithEvents m_cbo As MsForms.ComboBox

Private Sub m_cbo_Change()
MsgBox m_cbo.Value
End Sub


Insert a userform.
Add a few comboboxes
copy the code

''' Code for the userform
Option Explicit

'holds the (classed) objects
Dim colCombos As Collection

Private Sub UserForm_Initialize()
Dim ctl As MsForms.Control
Dim subCBO As clsCombo

Set colCombos = New Collection

For Each ctl In Me.Controls
If TypeName(ctl) = "ComboBox" Then
'dummy populate
ctl.List = Array(1 & ctl.Name, 2 & ctl.Name, 3 & ctl.Name)

'create(instantiate) a copy of your class
Set subCBO = New clsCombo
'assign the ctl to the class's combobox object
Set subCBO.m_cbo = ctl
'add to collection to keep the instance alive
colCombos.Add subCBO
End If
Next

End Sub


--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Fendic wrote :


Hi all,

I've got questions again I've got a userform with about 30
checkBoxes. If any of the checkBoxes is ticked or unticked, I'd like
the same block of code to be executed. How can I write smart and tidy
code instead of writing 30 checkBox1_Change() ? any idea? Thanks

All the best,
Fendic

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default smart & tidy code for many checkBox_Change()

oops checkboxes not comboboex.. :)

Option Explicit

''' Code for ClassModule Named clsCheck
Public WithEvents m_chk As MsForms.CheckBox

Private Sub m_chk_Change()
Debug.Print m_chk.Name, m_chk.Value
End Sub


'holds the (classed) objects
Dim colChecks As Collection

Private Sub UserForm_Initialize()
Dim ctl As MsForms.Control
Dim subCHK As clsCheck

Set colChecks = New Collection

For Each ctl In Me.Controls
If TypeName(ctl) = "CheckBox" Then
'create(instantiate) a copy of your class
Set subCHK = New clsCheck
'assign the ctl to the class's Checkbox object
Set subCHK.m_chk = ctl
'add to collection to keep the instance alive
colChecks.Add subCHK
End If
Next

'initialize the state of the checkboxes
SetFlags 5

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
MsgBox "The values of the checkboxes a " & GetFlags
End Sub

Sub SetFlags(Flags As Long)
Dim lng&, n&, chk As clsCheck
For Each chk In colChecks
chk.m_chk = CBool(Flags And 2 ^ n)
n = n + 1
Next
End Sub

Function GetFlags() As Long
Dim lng&, n&, chk As clsCheck
For Each chk In colChecks
If chk.m_chk Then
lng = lng + 2 ^ n
End If
n = n + 1
Next
GetFlags = lng
End Function



the last code is an (imo) efficient way to pass the
the values of a series of booleans to and from your form




--
keepITcool
| www.XLsupport.com | keepITcool chello nl | amsterdam


Fendic wrote :


Hi all,

I've got questions again I've got a userform with about 30
checkBoxes. If any of the checkBoxes is ticked or unticked, I'd like
the same block of code to be executed. How can I write smart and tidy
code instead of writing 30 checkBox1_Change() ? any idea? Thanks

All the best,
Fendic

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default smart & tidy code for many checkBox_Change()


Excellent, class module is exactly what i am looking for! thanks so muc
keepsITCool ;

--
Fendi
-----------------------------------------------------------------------
Fendic's Profile: http://www.excelforum.com/member.php...fo&userid=2395
View this thread: http://www.excelforum.com/showthread.php?threadid=39365

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
Tidy up multiple find and replace code PSM[_10_] Excel Worksheet Functions 2 April 6th 09 02:00 PM
Tidy lookup PBcorn Excel Worksheet Functions 2 June 18th 08 08:05 AM
Tidy Up Pete Excel Discussion (Misc queries) 4 May 9th 05 05:09 PM
Tidy up data Al Mackay Excel Programming 3 April 27th 04 02:16 PM
Tidy up macro! Gareth[_3_] Excel Programming 3 September 18th 03 06:48 PM


All times are GMT +1. The time now is 07:25 AM.

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

About Us

"It's about Microsoft Excel"