Derpout
Derpout

Reputation: 13

Concatenation for checkboxs name in a userform

I have 40 checkboxs in a userform, named ck1 to ck40 because I need to check if they're checked or not but it doesn't work usign a loop. I tried things like :

For i = 1 To 40

       If Me.ck & i.Value = True Then
             ticked = True
             Exit For
      End If

Next i

or Me.ck & i & .Value or many other things. I also tried to use Set but none of my aptents worked because, the program stops at Me.ck. I haven't found any solutions using concatenation.. I obviously have to use Me. because my code is in the userform

I'm looking for any solution

Upvotes: 1

Views: 225

Answers (2)

99moorem
99moorem

Reputation: 1983

Why are you exiting the for? your code would go through and do the first one and then come out of the loop? Below should be ok

For i = 1 To 40
    If Me.Controls("ck" & i).Value Then
        Me.Controls("ck" & i).value = True
    End If
Next i

If you wanted something a little more dynamic you could use

Dim cCont As Control
For Each cCont In Me.Controls
    If TypeName(cCont) = "CheckBox" Then
        cCont.value = True
    End If
 Next cCont

Upvotes: 0

mielk
mielk

Reputation: 3940

Try the code below:

For i = 1 To 40
    If Me.Controls("ck" & i).Value Then
        ticked = True
        Exit For
    End If
Next i

Upvotes: 2

Related Questions