I am trying to loop through two sets of controls on a userform. If a combination of two controls (togglebuttons) are true then do something. The sub is called from the userform where the controls are created. This is the first time I've used the userform control variable, the code below won't compile so this is probably completely wrong. Any suggestions please?
Updated the code sample, but I am getting a compile error. See image below:
Private Sub CONTROLS_Click()
Dim X As Integer, Y As Integer
Dim BOOK As Variant, STRAT As Variant
BOOK = Array("TYPE1", "TYPE2")
STRAT = Array("FAST", "MEDIUM", "SLOW")
For X = LBound(BOOK) To UBound(BOOK)
If Me.CONTROLS(BOOK(X)).Value = True Then
For Y = LBound(STRAT) To UBound(STRAT)
If Me.CONTROLS(STRAT(Y)).Value = True Then
'DO SOMETHING
End If
Next Y
End If
Next X
End Sub
These are each an array of 1.
BOOK = Array("TYPE1, TYPE2")
STRAT = Array("FAST,MEDIUM,SLOW")
They should be,
BOOK = Array("TYPE1", "TYPE2")
STRAT = Array("FAST", "MEDIUM", "SLOW")
Variant arrays are typically zero-based; the first element in the BOOK array is at BOOK(0). They use a 1 based index when filled directly off a worksheet for if redimmed explicitly to start at 1. Best to use the LBound function and UBound function to determien the Lower and Upper Boundaries all of the time.
Private Sub CONTROLS_Click()
Dim X As Integer, Y As Integer
Dim BOOK As Variant, STRAT As Variant
'Type1, Type2, Fast, Medium and Slow are the names of the togglebuttons
BOOK = Array("TYPE1", "TYPE2")
STRAT = Array("FAST", "MEDIUM", "SLOW")
For X = LBound(BOOK) To UBound(BOOK)
If Me.CONTROLS(BOOK(X)).Value = True Then
For Y = LBound(STRAT) To UBound(STRAT)
If Me.CONTROLS(STRAT(Y)).Value = True Then
'DO SOMETHING
End If
Next Y
End If
Next X
End Sub
That should fix your array assignment and nested For ... Next
loops.