ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop for checkboxes (https://www.excelbanter.com/excel-programming/287733-loop-checkboxes.html)

matt

Loop for checkboxes
 
Is it possible in VBA to do a typical loop? Such as this. Basically I want checkboxes 2-5 to be invisible if checkbox 1 is not checked. I've found that it will not allow me to put in the "x + 1". Thanks. Matt

const x =
If checkbox1.value = fals
do while x <
checkbox(x).visible = fals
x +
loo
end if

Bob Phillips[_6_]

Loop for checkboxes
 
What sort of checkboxes, userform, forms toolbar, or control toolbar?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Matt" wrote in message
...
Is it possible in VBA to do a typical loop? Such as this. Basically I

want checkboxes 2-5 to be invisible if checkbox 1 is not checked. I've
found that it will not allow me to put in the "x + 1". Thanks. Matt.

const x = 2
If checkbox1.value = false
do while x < 5
checkbox(x).visible = false
x + 1
loop
end if




matt

Loop for checkboxes
 
Control Checkboxes. Matt

Rob

Loop for checkboxes
 
I'd like to know how to do this on a Userform.
-----Original Message-----
What sort of checkboxes, userform, forms toolbar, or

control toolbar?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Matt" wrote in

message
...
Is it possible in VBA to do a typical loop? Such as

this. Basically I
want checkboxes 2-5 to be invisible if checkbox 1 is not

checked. I've
found that it will not allow me to put in the "x + 1".

Thanks. Matt.

const x = 2
If checkbox1.value = false
do while x < 5
checkbox(x).visible = false
x + 1
loop
end if



.


Jake Marx[_3_]

Loop for checkboxes
 
Hi Matt,

Something like this should work:

Private Sub CheckBox1_Click()
Dim ole As OLEObject

For Each ole In OLEObjects
If TypeOf ole.Object Is MSForms.CheckBox Then
If ole.Name < "CheckBox1" Then
ole.Visible = CheckBox1.Value
End If
End If
Next ole
End Sub

It loops through all OLEObjects, and if the current OLEObject in the loop is
a CheckBox, it will set its Visible property according to the value of
CheckBox1.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Matt wrote:
Control Checkboxes. Matt



matt

Loop for checkboxes
 
Jake, thanks. This helps, but I forgot to mention that I have other control checkboxes on the worksheet that I don't want to be dependent on checkbox1. I just want a selection dependent on the first checkbox. Any ideas? Thanks. Matt

Jake Marx[_3_]

Loop for checkboxes
 
Matt,

If your CheckBoxes are named "CheckBox1", "CheckBox2", and so on, you can
use this:

Private Sub CheckBox1_Click()
Dim ole As OLEObject
Dim nChkNum As Integer

For Each ole In OLEObjects
If TypeOf ole.Object Is MSForms.CheckBox Then
nChkNum = CInt(Mid$(ole.Name, Len("CheckBox") + 1))
If nChkNum = 2 And nChkNum <= 5 Then
ole.Visible = CheckBox1.Value
End If
End If
Next ole
End Sub

If not, you will have to change the If statement to something like this:

If ole.Name="Mychk2" Or ole.Name="check3" Or ole.Name="checkbox04" Then

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Matt wrote:
Jake, thanks. This helps, but I forgot to mention that I have other
control checkboxes on the worksheet that I don't want to be dependent
on checkbox1. I just want a selection dependent on the first
checkbox. Any ideas? Thanks. Matt



Jake Marx[_3_]

Loop for checkboxes
 
Matt,

Please try to keep a question in the same thread. I didn't see your thread
on this subject posted 1 1/2 hours prior to this one (and those answering
your other thread probably didn't see this one), so a lot of duplicated work
resulted.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Matt wrote:
Is it possible in VBA to do a typical loop? Such as this. Basically
I want checkboxes 2-5 to be invisible if checkbox 1 is not checked.
I've found that it will not allow me to put in the "x + 1". Thanks.
Matt.

const x = 2
If checkbox1.value = false
do while x < 5
checkbox(x).visible = false
x + 1
loop
end if



Bob Phillips[_6_]

Loop for checkboxes
 
Rob,

This is the sort of thing that you want

Dim ctl As MSForms.Control

For Each ctl In Me.Controls
If TypeName(ctl) = "CheckBox" Then
If ctl.Value < True Then
ctl.Visible = False
End If
End If
Next ctl


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Rob" wrote in message
...
I'd like to know how to do this on a Userform.
-----Original Message-----
What sort of checkboxes, userform, forms toolbar, or

control toolbar?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Matt" wrote in

message
...
Is it possible in VBA to do a typical loop? Such as

this. Basically I
want checkboxes 2-5 to be invisible if checkbox 1 is not

checked. I've
found that it will not allow me to put in the "x + 1".

Thanks. Matt.

const x = 2
If checkbox1.value = false
do while x < 5
checkbox(x).visible = false
x + 1
loop
end if



.





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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com