how to use object variable to change the property of a

  • Thread starter Thread starter Fendic
  • Start date Start date
F

Fendic

Hi all,

I'd like to like to assign a number of Labels into an array, so that
can use loop to change the caption of each Label. I tried testing b
using object variable as follows:

Dim testLabel As Label
testLabel = myLabel

myLabel is a Label i created in a form. but if i run the code, an erro
would show that ''object variable or 'with' block variable is no
defined'', which i don't understand, cos apparently testLabel i
clearly defined.

However, if i change it to be:

Dim testLabel
testLabel = myLabel

it works, but testLabel is actually a string, in stead of a label, thu
i'll not be able to change the caption of it.

can anyone help please. it sounds easy, but i've been struggling fo
days, and just can't get the syntax right. thanks

All the best,
Fendi
 
Dim ctrl as Control
i = 1
for each ctrl in Userform1.Controls
if typeof ctrl is MSForms.Label then
ctrl.Caption = "Item" & i
i = i + 1
end if
Next
 
The code is working, but there's still one problem that it changes ALL
labels in the user form. What if i only want to change some of them?(10
out of 21) that's why i want to assign them into an array, so that i can
get control only part of them. I appreciate your help, Tom. any idea
about it?
 
Dim arrLbl(1 to 10) as MsForms.Label
for i = 1 to 10
set arrLbl(i) = Userform1.Controls("Label" & i)
Next

for i = 1 to 10
arrLbl(i).Caption = "Item" & i
Next


as an example. You would have to work out how to identify which labels to
assign to the array. the above is just an example.
 
Back
Top