ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   UserForm Query (https://www.excelbanter.com/excel-programming/436570-userform-query.html)

Derek Dowle

UserForm Query
 
UserForm Query

I have a User Form which is used to collect monthly cost information for a
five year period for a database. The start month and year of the five year
period will vary from record to record.

Intially the user enters the start month and start year into separate Text
Boxes. From this information I have created an Array, strMonthLabels(60),
which contains each month and year for the five year period; i.e. "Jan 2009",
"Feb 2009", etc.

Each of the Text Boxes to capture the month cost information has an adjacent
Label to signify the month and year so that the user can enter the data
correctly. These Labels are named lblMonth1 through to lblMonth60.

I want to be able to use a For-Next Loop to change the Captions on each of
the Labels. The following indicates my intententions but does not work.

For i = 1 To 60
lblMonth(i).Caption = strMonthLabels(i)
Next

An alternative I have tried unsuccessfully is to create a Label variable

Dim objLabelName As Label

and then a value for this variable

objLabelName = "lblMonth" + Trim(Str(i))
objLabelName.Caption = strMonthLabels(i)

The answer is probably staring me in the face!

Please can you advise how to use a For-Next Loop to move from Label to Lable
to change each caption?

Many thanks

--
Derek Dowle

p45cal[_190_]

UserForm Query
 
For i = 1 To 60
UserForm1.Controls("lblMonth" & i).Caption = strMonthLabels(i)
Next i

-userfom1-(or your userform's name) may/may not be necessary depending
on where the code is placed.


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=156812

Microsoft Office Help


Jon Peltier[_2_]

UserForm Query
 
Note that objLabelName is a string, so you need this:

controls(objLabelName).caption = strMonthLabels(i)

But p45cal's code doesn't need an intermediate string.

- Jon
-------
Jon Peltier
Peltier Technical Services, Inc.
http://peltiertech.com/



Derek Dowle wrote:
UserForm Query

I have a User Form which is used to collect monthly cost information for a
five year period for a database. The start month and year of the five year
period will vary from record to record.

Intially the user enters the start month and start year into separate Text
Boxes. From this information I have created an Array, strMonthLabels(60),
which contains each month and year for the five year period; i.e. "Jan 2009",
"Feb 2009", etc.

Each of the Text Boxes to capture the month cost information has an adjacent
Label to signify the month and year so that the user can enter the data
correctly. These Labels are named lblMonth1 through to lblMonth60.

I want to be able to use a For-Next Loop to change the Captions on each of
the Labels. The following indicates my intententions but does not work.

For i = 1 To 60
lblMonth(i).Caption = strMonthLabels(i)
Next

An alternative I have tried unsuccessfully is to create a Label variable

Dim objLabelName As Label

and then a value for this variable

objLabelName = "lblMonth" + Trim(Str(i))
objLabelName.Caption = strMonthLabels(i)

The answer is probably staring me in the face!

Please can you advise how to use a For-Next Loop to move from Label to Lable
to change each caption?

Many thanks



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

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