Check Box checking

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Hi

I have 12 check boxes for each month.

Is there a quick way of looping through all the check boxes to see if they
are ticked or not?

I then want to write the info to an array.

Thanks

Steve
 
You want to know what? Which ones are checked, how many are checked, if any
at all are checked, if none are checked. Pick one.
 
Which ones are checked

JLGWhiz said:
You want to know what? Which ones are checked, how many are checked, if any
at all are checked, if none are checked. Pick one.
 
Where are the CheckBoxes at... directly on the worksheet or on a UserForm?
If on the worksheet, where did they come from... the Forms Toolbar or the
Visual Basic toolbar?

Rick
 
Apologies

They are on a user form

Rick Rothstein (MVP - VB) said:
Where are the CheckBoxes at... directly on the worksheet or on a UserForm?
If on the worksheet, where did they come from... the Forms Toolbar or the
Visual Basic toolbar?

Rick
 
Something like this maybe (using a CommandButton to enact the code for
example purposes only)...

Private Sub CommandButton1_Click()
Dim Cnt As Long
Dim Ctrl As Control
Dim ChkBxArray() As String
ReDim ChkBxArray(1 To 12)
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.CheckBox Then
If Ctrl.Value Then
Cnt = Cnt + 1
ChkBxArray(Cnt) = Ctrl.Name
End If
End If
Next
ReDim Preserve ChkBxArray(1 To Cnt)
' Prove it worked
For Cnt = 1 To UBound(ChkBxArray)
Debug.Print ChkBxArray(Cnt)
Next
End Sub

Rick
 
Hi Steve,

Try something like:


'==========>>
Option Explicit
Dim arr(1 To 12) As Boolean

'------------>>
Private Sub CommandButton1_Click()
Dim Ctrl As msforms.Control
Dim i As Long

For Each Ctrl In Me.Controls
With Ctrl
If TypeOf Ctrl Is msforms.CheckBox Then
i = i + 1
arr(i) = .Value
End If
End With
Next Ctrl

End Sub

'------------>>
Private Sub CommandButton2_Click()
MsgBox arr(1)
End Sub
'<<==========
 
Many Thanks Rick

I'll give it a go

Steve

Rick Rothstein (MVP - VB) said:
Something like this maybe (using a CommandButton to enact the code for
example purposes only)...

Private Sub CommandButton1_Click()
Dim Cnt As Long
Dim Ctrl As Control
Dim ChkBxArray() As String
ReDim ChkBxArray(1 To 12)
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.CheckBox Then
If Ctrl.Value Then
Cnt = Cnt + 1
ChkBxArray(Cnt) = Ctrl.Name
End If
End If
Next
ReDim Preserve ChkBxArray(1 To Cnt)
' Prove it worked
For Cnt = 1 To UBound(ChkBxArray)
Debug.Print ChkBxArray(Cnt)
Next
End Sub

Rick
 
Many Thanks Norman

I'll give it a go.

Appreciate your time.

regards

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top