+ Reply to Thread
Results 1 to 2 of 2

calculating object names (e.g. ".Labelxx.Caption")

  1. #1
    Registered User
    Join Date
    05-04-2005
    Posts
    2

    calculating object names (e.g. ".Labelxx.Caption")

    Hello all,

    I am currently working on a user form that shall be used to display the contents of the active row.

    Thus I created a form that contains e.g. 20 Labels and 20 TextBoxes. The names of the fields are currently 'Label1', 'Label2' ... 'Label20' and 'TextBox1', 'TextBox2' ... 'TextBox20'.

    The values of the Labels and Textboxes shall be set with the 'Initialize' function.

    As I have 20 Labels I am currently trying to figure out the best way to set the values when the form is loaded.

    Is there any chance to refer to the objects by using them with FOR ... NEXT for example like this:

    FOR x = 1 to 20
    MyForm.Label(x).Caption = ActiveSheet.Cells(1, x).Value
    MyForm.Label(x).Text =ActiveSheet.Cells(ActiveCell.Row, x).Value
    NEXT

    This would be much easier than defining the values separately for each Label and TextBox.

    I already tried to define a (string) variable that combines the correct object name (e.g. vLabel = "MyForm.Label" + Str(x) + ".Caption") but this does not work as this is not an object.

    Could you please give me a hint ?

    Many thanks in advance for your help !

    Claus.

    P.S.: I am pretty new to VBA, so please excuse if this is a dumb question.

  2. #2
    Registered User
    Join Date
    05-04-2005
    Posts
    2

    Post

    YIPPIEE !!

    I just found the correct syntax in another thread (many thanks to Tom Ogilvy):

    For x = 1 To 20
    MyForm.Controls("Label" & x).Caption = ActiveSheet.Cells(1, x).Value
    MyForm.Controls("Textbox" & x).Value = ActiveSheet.Cells(ActiveCell.Row, x).Value
    Next

    Now this looks easy ....

    You guys are great !

    Thanks,
    Claus.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1