Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel macro to change checkboxes by groupname 1

Status
Not open for further replies.

osx99

Technical User
Apr 9, 2003
250
GB
I can easily change individual checkboxes based on results of another using

Code:
Private Sub CheckBox202_Click()
    If CheckBox202.Value = True Then
        CheckBox203.Value = False
        CheckBox204.Value = False
        CheckBox205.Value = False
        CheckBox206.Value = False
    End If
End Sub

What is the syntax for changing nore than one checkbox by using groupname

E.g I have groups of checkboxes - one of which the groupname is 'primary_chkbox'

Can I change all checkboxes with this groupname in one statement

eg. primary_chkbox.value = False ?
 
GroupName is a property returning string, you need another test:
Code:
If CheckBox203.GroupName="primary_chkbox" Then CheckBox203.Value = False
or a loop for all:
Code:
For Each ctrl In Me.Controls
  If TypeName(ctrl)="CheckBox" Then
    If ctrl.GroupName="primary_chkbox" then
      ctrl.Value=False
    End If
  End If
Next ctrl

combo
 
I get 'Compile Error' Method or Data Member not found

Is this because I'm using control toolbox checkboxes rather than forms?

 

I've tried the following

Code:
Dim oleObj As OLEObject

For Each oleObj In ActiveSheet.OLEObjects
If TypeOf oleObj.Object Is MSforms.CheckBox Then
    If oleObj.GroupName = "Primary_checkbox" Then
    oleObj.Object.Value = False

    End If
End If
Next
[code/]
but 'If oleObj.GroupName = "Primary_checkbox" Then' gives an error
 

DOh - SOLVED

Code:
Private Sub CheckBox190_Click()


Dim oleObj As OLEObject

For Each oleObj In ActiveSheet.OLEObjects
If TypeOf oleObj.Object Is MSforms.CheckBox Then
    If oleObj.Object.GroupName = "Primary_checkbox" Then
    oleObj.Object.Value = False

    End If
End If
Next



End Sub
 
Nice, I assumed you work with controls on userform.

combo
 



Hi,

Please post future VBA code questions in Forum707. This forum is not designed to address code issues.

Thanks.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top