0

I am attempting to count the number of check boxes that have been checked on a specific page of a userform. I am using VBA with Excel 2010.

This is my code:

Public Sub AutoCount()
Dim ctl As control
Dim j As Long

For Each ctl In CharacterBuilder.MultiPage1.Pages(2).Controls' error occurs here
    If TypeOf ctl Is msforms.CheckBox Then
    If CharacterBuilder.Controls(ctl.name).Value = True Then
        j = j + 1
    End If
    End If
Next
CharacterBuilder.Remaining.Caption = j
Unload CharacterBuilder.Skills 'Not sure what this does
End Sub

I get the run-time error 459 Object or Class Does not Support the set of events.

ale10ander
  • 942
  • 5
  • 22
  • 42

2 Answers2

0

It's a peculiarity with the MultiPage control that, unlike most things VBA, its indices are 0-based.

Therefore your second page would be Pages(1) and not Pages(2), since Pages(0) is the first/default page. If you change your error line to the following:

For Each ctl In CharacterBuilder.MultiPage1.Pages(1).Controls

your code should then work.

djikay
  • 10,450
  • 8
  • 41
  • 52
  • I appreciate the reply, but I'm not sure that's the issue. This multipage has six pages (and I was accounting for 0-based indexing). Does this code work on your system? Could it have to do with the fact that I have this code in a module? – ale10ander Jul 28 '14 at 22:43
  • @Cptn_Hammer: Strange. Yes, it works as expected on my system. I literally just tried it (again!) from a module and it behaved correctly. – djikay Jul 28 '14 at 22:51
0

the reason might be because you haven't defined tour control variable properly. you could try

Dim Ctl as MSForms.control