View Full Version : Need Help using array(i) with user form label

04-07-2012, 12:05 PM
What i did is compare an array with a spreadsheet column, then if they are equal it saves it as 1. Then when the user opens the user form and clicks a button it goes through the comparisn and if it sees that its = 1 then it will change the backround color of a label on the object.

but it seems that the array and object's label are dimmed differently or something, cause i am getting an error of object variable or with block variable not set. please help

heres the code

Private Sub CommandButton3_Click()
Dim i As Variant
Dim s As Object
For i = 100 To 883
If check(i) = 1 Then
s = slist(i)
With s
.BackColor = RGB(0, 500, 0)
End With
End If
Next i
End Sub

oh by the way i have the slist() array saved as a global variant in the module.
when i saved the slist () as a global object then i get an error when i fill the array. i have not dimmed anything an object before so please explain

04-07-2012, 03:12 PM
I've no idea what's on slist, but try:
Set s = slisyt(i)

04-07-2012, 04:28 PM
Hey when i tried that it gave me an Object required error.
heres the code that fills the array, the label has a "s" in front of the nubmer so this is how i put the s in while saving the nubmers, not sure if thats causing this problem

Private Sub CommandButton3_Click()
Dim i As Variant
For i = 1 To 880
slist(i) = "s" & i
Next i

04-07-2012, 05:13 PM
members of slist are strings, not lables. With Me.Controls(slist(i))

04-08-2012, 12:16 PM
Hey mike that worked thanks... Can u explain why tho, I haven't used the me.controls before.


04-08-2012, 01:54 PM
"Label1" is a string not a label.
This code will fail, since the variable LabelName isn't an object and it doesn't have a .Caption property.
' will fail

Dim lableName as String
LabelName = "Label1"

LableName.Caption = "a caption"

Given a string, the .Controls property of a useform object will return the named control.
' will work

Dim lableName as String
LabelName = "Label1"

UserForm1.Controls(LableName).Caption = "a caption"