1

Iam creating my labels and checkbox by code:

i = 1
While Not Sheets("I_M_1_1PW").Cells(9 + i, 43) = "koniec"

    Set theLabel = UserForm1.Controls.Add("Forms.Label.1", labelCounter, True)
    With theLabel
        .Caption = Sheets("I_M_1_1PW").Cells(9 + i, 43)
        .Left = 10
        .Width = 100
        .Top = 13 * labelCounter
        Debug.Print labelCounter & "   " & theLabel.Caption
    End With

Set chkbox = UserForm1.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
    chkbox.Caption = Sheets("I_M_1_1PW").Cells(9 + i, 44)
    chkbox.Left = 100
    chkbox.Width = 75
    chkbox.Top = 13 * labelCounter
i = i + 1
labelCounter = labelCounter + 1

I can search for active check box by code:

For j = 1 To Granica - 1

            If UserForm1.Controls("CheckBox_" & j).Value = True Then

             Wynik1 = UserForm1.Controls("CheckBox_" & j).Caption + Wynik1
    '*         Wzorce = Wzorce + UserForm1.label(j).Caption

            End If
Next

But in '* place i got problem, cant take label.caption when iam using UserForm1.Controls(j).Caption its looping through all parts parts of user.form not only labels.

1
  • I think it is not a good idea to give a simple numeric value (ie: labelCounter) as name for your labels. Maybe build a name like you do for checkboxes: UserForm1.Controls.Add("Forms.Label.1", "label_" & labelCounter, True). Commented May 4, 2016 at 7:19

3 Answers 3

3

you have to use

Wzorce = Wzorce + UserForm1.Controls(CStr(j)).Caption

here follows some other possible enhancements of your code

Dim theLabel As MSForms.Label, chkbox As MSForms.CheckBox
Dim Wynik1 As Variant, Wzorce As Variant

i = 1
While Not Sheets("I_M_1_1PW").Cells(9 + i, 43) = "koniec"

    Set theLabel = UserForm1.Controls.Add("Forms.Label.1", i, True)
    With theLabel
        .Caption = Sheets("I_M_1_1PW").Cells(9 + i, 43)
        .Left = 10
        .Width = 100
        .Top = 13 * i
        Debug.Print i & "   " & theLabel.Caption
    End With

    Set chkbox = UserForm1.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
    With chkbox
        .Caption = Sheets("I_M_1_1PW").Cells(9 + i, 44)
        .Left = 100
        .Width = 75
        .Top = 13 * i
    End With

    i = i + 1
Wend

...

For j = 1 To Granica - 1       

  If UserForm1.Controls("CheckBox_" & j).Value = True Then
         Wynik1 = chkboxes(j).Caption + Wynik1
         Wzorce = Wzorce + UserForm1.Controls(j).Caption    
  End If
Next

Finally here follows same code above but with exploitation of control arrays, which can possibly make your code more readable and maintainable:

Dim theLabel As MSForms.Label, chkbox As MSForms.CheckBox
Dim labels() As MSForms.Label, chkboxes() As MSForms.CheckBox

i = 1
While Not Sheets("I_M_1_1PW").Cells(9 + i, 43) = "koniec"

    Set theLabel = UserForm1.Controls.Add("Forms.Label.1", i, True)
    With theLabel
        .Caption = Sheets("I_M_1_1PW").Cells(9 + i, 43)
        .Left = 10
        .Width = 100
        .Top = 13 * i
        Debug.Print i & "   " & theLabel.Caption
    End With
    ReDim Preserve labels(1 To i)
    Set labels(i) = theLabel

    Set chkbox = UserForm1.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
    With chkbox
        .Caption = Sheets("I_M_1_1PW").Cells(9 + i, 44)
        .Left = 100
        .Width = 75
        .Top = 13 * i
    End With
    ReDim Preserve chkboxes(1 To i)
    Set chkboxes(i) = chkbox

    i = i + 1
Wend

...

For j = 1 To Granica - 1
    If chkboxes(j).Value = True Then
        Wynik1 = chkboxes(j).Caption + Wynik1
        Wzorce = Wzorce + labels(j).Caption
    End If
Next

End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming your controls are in pairs, it should be:

Wzorce = Wzorce + UserForm1.Controls("" & j).Caption

though I think you should name the labels the same way you did the checkboxes.

3 Comments

Is "" & j better than CStr(j) ?
@Rory: sorry, I was editing my answer while you already posted yours. and as long the OP's question is concerned they gave the same solution (though in different "flavours") -> credits to you!
@VincentG No, not better or worse.
0

Great it works.

Wzorce = Wzorce + UserForm1.Controls("label_" & j).Caption

Ty user3598756.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.