ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Userform Label question (https://www.excelbanter.com/excel-programming/315208-userform-label-question.html)

David Goodall

Userform Label question
 
Hello,

I have a form which contains 10 labels, named Lb1 to Lb10. The form has
a combobox and a command button. What I would like to is each time the
command button is clicked the text in combobox is added to the labels.

I've tried the following but it doesnt work.
sub commandbutton_click()
dim count as integer
dim lablename as string

count = 1

lablename = CStr(count) ' I thought I would need to cast the variable

Lb & lablename.caption = cblist.text

count = count + 1

' I will need to create a loop so that the count stops at 10.
end sub

Also I'm sure there's a much better way to do this. Could you create an
array of label names then loop through them using count?

Any help as always greatly appreciated.

David

Tom Ogilvy

Userform Label question
 
sub commandbutton_click()
Static count as integer
dim lablename as string

if count = 0 then _
count = 1

if count 10 then
count = 1
lablename = CStr(count) ' I thought I would need to cast the variable

Userform1.Controls("Lb" & lablename).caption = cblist.text

count = count + 1
End Sub


or if you want the same value placed in all labels:


sub commandbutton_click()
Dim c count as integer
dim lablename as string

for count = 1 to 10

lablename = CStr(count) ' I thought I would need to cast the variable

Userform1.Controls("Lb" & lablename).caption = cblist.text

Next

End Sub

--
Regards,
Tom Ogilvy



"David Goodall" wrote in message
...
Hello,

I have a form which contains 10 labels, named Lb1 to Lb10. The form has
a combobox and a command button. What I would like to is each time the
command button is clicked the text in combobox is added to the labels.

I've tried the following but it doesnt work.
sub commandbutton_click()
dim count as integer
dim lablename as string

count = 1

lablename = CStr(count) ' I thought I would need to cast the variable

Lb & lablename.caption = cblist.text

count = count + 1

' I will need to create a loop so that the count stops at 10.
end sub

Also I'm sure there's a much better way to do this. Could you create an
array of label names then loop through them using count?

Any help as always greatly appreciated.

David




David Goodall

Userform Label question
 
Tom
Thank you very much.
David




Tom Ogilvy wrote:
sub commandbutton_click()
Static count as integer
dim lablename as string

if count = 0 then _
count = 1

if count 10 then
count = 1
lablename = CStr(count) ' I thought I would need to cast the variable

Userform1.Controls("Lb" & lablename).caption = cblist.text

count = count + 1
End Sub


or if you want the same value placed in all labels:


sub commandbutton_click()
Dim c count as integer
dim lablename as string

for count = 1 to 10

lablename = CStr(count) ' I thought I would need to cast the variable

Userform1.Controls("Lb" & lablename).caption = cblist.text

Next

End Sub



All times are GMT +1. The time now is 10:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com