Home Checkbox to select various months
Reply: 2

Checkbox to select various months

user8958702
1#
user8958702 Published in 2017-12-07 18:00:52Z

Within a userform, I'm coding a checkbox to select multiple other checkboxes (months). This is to enable the user to select all 12 months rather than clicking through each of them. Following is the code I've written. What have I done wrong now?

Private Sub Selectbox_AfterUpdate()
Dim x As Integer

If Me.Selectbox.Value = True Then
For x = 0 To 12
Me.Controls("Month" & x).Value = True
Next x
Me.Selectbox.Caption = "Deselect All"

ElseIf Me.Selectbox.Value = False Then
For x = 0 To 12
Me.Controls("Month" & x).Value = False

Next x
Me.Selectbox.Caption = "Select All"

End If

End Sub
negas
2#
negas Reply to 2017-12-07 18:55:19Z

There are 12 month. Change

For x = 0 To 12

to

For x = 1 To 12

It's depend the name you are using.

Brian M Stafford
3#
Brian M Stafford Reply to 2017-12-07 18:59:49Z

Don't check against True or False, check against vbChecked or vbUnchecked. You also have 1 too many iterations on your loop. Your code would look like this:

Private Sub Selectbox_AfterUpdate()
   Dim x As Integer

   If Me.Selectbox.Value = vbChecked Then
      For x = 1 To 12
         Me.Controls("Month" & x).Value = vbChecked
      Next x

      Me.Selectbox.Caption = "Deselect All"
   ElseIf Me.Selectbox.Value = vbUnchecked Then
      For x = 1 To 12
         Me.Controls("Month" & x).Value = vbUnchecked
      Next x

      Me.Selectbox.Caption = "Select All"
   End If
End Sub
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.319257 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO