AndroidDev
AndroidDev

Reputation: 691

Loop to Create Option buttons in a Userform not working

I need to create several option buttons in a Userform (each one for a array element).

Am I missing something here? Cause it returns an error..

DefaultTop = 60

For X = 0 To UBound(ArrPlants)

    Set Jonas(x) = SAPPlants.Controls.Add("Forms.optionbutton.1", Y, True)
      'Error on the above line ^^^

        With Jonas(x)
            .Top = DefaultTop + 20
            .Left = 12
            .Caption = ArrPlants(x)
        End With

 Next X

Error:

Error Expected Sub or Procedure

Upvotes: 1

Views: 503

Answers (1)

Chrismas007
Chrismas007

Reputation: 6105

You need to pass X into the name of the OptionButton instead of using Jonas(X). Try the below and see how that works for you:

DefaultTop = 60

Dim Jonas as OptionButton

For X = 0 To UBound(ArrPlants)

    Set Jonas = SAPPlants.Controls.Add("Forms.optionbutton." & X + 1, Y, True)

        With Jonas
            .Top = DefaultTop + 20
            .Left = 12
            .Caption = ArrPlants(X)
        End With

 Next X

Further explanation found here:

enter image description here

Upvotes: 1

Related Questions