Construct label names from strings and set them?

S

Sophia

I *know* this is a dumb question, but ...

I have a form with 6 labels on it. The "Name" Property of the labels
is "lable1" through "label6"

I would like to create a loop that sets the Captions of the six
labels, using the contents of an array of strings named "Captions(1
to 6)".

Like: Label1.Caption = Captions(1)

I've tried assembling the variable names from strings, but VBA doesn't
like it ... so:

For LabelNumber 1 to 6

Somebody, please fill this in.

Next LabelNumber

TIA
 
L

Lars-Eric Gisslén

Sophia,

Perhaps you could try something like this:
'---------------------
Private Sub ChangeCaptions()
Dim i As Long

For i = 1 To 6
SetLabelCaption "Label" & Format(i), "Caption " & Format(i)
Next

End Sub
'---------------------
Private Sub SetLabelCaption( _
ByVal sName As String, _
ByVal sCaption As String)

Dim oCtrl As Control

For Each oCtrl In Me.Controls
If oCtrl.Name = sName Then
oCtrl.Caption = sCaption
Exit For
End If
Next
End Sub
'---------------------

Regards,
Lars-Eric
 
S

Sophia

Lars-Eric,

Very neat ... no wonder I had trouble with what I was doing. I
appreciate your help very much.

Regards,

Sophia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top