Array of labels??

  • Thread starter Thread starter jaxrpc
  • Start date Start date
J

jaxrpc

Hi i am trying to create an array of labels in excel userforms is thi
possible in excel vba?
i know it is possible in vb.

e.g like the user input 5 in an input box i will horizotally create
labels on the forms with the number 1 to 5 on each of the labels.

thanks alo
 
Hi Jaxrpc,

Arrays of controls are not supported, although there are ways around it.

If you have 5 labels called Label1,Label2...Label5 then you could
reference them with syntax like this,

For intIndex = 1 to 5
Controls("Label" & intIndex).Caption = "Caption " & intIndex
Next

Or you could create your own array. At the top of the useform code
module declare an array, in the Initialize event assign the labels to
your array and final use this syntax to change properties.

Private LabelArray_A(5) As MSForms.Label

Private Sub UserForm_Initialize()
Set LabelArray_A(1) = Label1
Set LabelArray_A(2) = Label2
Set LabelArray_A(3) = Label3
Set LabelArray_A(4) = Label4
Set LabelArray_A(5) = Label5
End sub

For intIndex = 1 To 5
LabelArray_A(intIndex).Caption = "Caption " & intindex
Next

Yet another option is to use a class object, which will also allow you
to handle events.

Add a new class to your project and include this statement;

Public WithEvents MyLabel As MSForms.Label

Declare your object array in the userform code module.

Private LabelArray_B(5) as New Class1

Then in the userform Initialize event assign the labels like this;

Set LabelArray_B(1).MyLabel = Label1
Set LabelArray_B(2).MyLabel = Label2
Set LabelArray_B(3).MyLabel = Label3
Set LabelArray_B(4).MyLabel = Label4
Set LabelArray_B(5).MyLabel = Label5

Use this syntax to change properties;

For intIndex = 1 To 5
LabelArray_B(intIndex).MyLabel.Caption = "Caption " & intIndex
Next


Hi i am trying to create an array of labels in excel userforms is this
possible in excel vba?
i know it is possible in vb.

e.g like the user input 5 in an input box i will horizotally create 5
labels on the forms with the number 1 to 5 on each of the labels.

thanks alot

--

Cheers
Andy

http://www.andypope.info
 
Back
Top