Changing a Label Caption at runtime from a data source

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Annie Bender
    New Member
    • Feb 2010
    • 15

    Changing a Label Caption at runtime from a data source

    Hi guys, brand new here as you can see. Brand new to VB too, although pretty good at HTML, asp, VBScript, Java script.

    This will seem simple to you guys, but I am stuck here! The intended application is the simple user box displayed in an Excel workbook, shown in the thumbnail below. It's all static content, nothing interactive, and I've got it put together except for one thing: I want the main text in the box to contain a volatile value, info that is taken from a cell in one of the worksheets. Right now, it's coded in at design time to say "September 23, 2010 or 146,240 miles" but I want that text line to change both the date and mileage reference in real time, taking the value from elsewhere in a cell of the worksheet. I've set the form up so that that text line is the caption of Label1. I've tried everything I can think of to reference the proper cell, but it either ignores my coding or it returns a compile or runtime error. Now I'm wondering if a Label control can't have a caption that draws its value from a data source, like a worksheet cell. Can you help, please? I'd really love some coding/syntax suggestions to accomplish this. TIA for your assistance.

    Annie
    Attached Files
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi. I have no problem setting the caption of a label control to a value in another cell, using for example

    Code:
    Label1.caption = Activesheet.Cells(1, 1) 
          or
    Label1.caption = Activesheet.Range("A1")
    to refer to cell A1 in the currently active worksheet. If the cell required is in another worksheet, you can retrieve it using

    Code:
    Label1.caption = Worksheets("YourSheetName").Range("A1")
    or similar.

    Welcome to Bytes!

    -Stewart

    Comment

    • Annie Bender
      New Member
      • Feb 2010
      • 15

      #3
      Thank you Stewart. I tried so many things, I must have tried that at some time, without success. Anyway, it works, but only if I include the form name as the first object, and the statement needs to precede the UserForm1.Show statement. That makes sense, I guess.

      UserForm1.Label 1.Caption = Worksheets("You rSheetName").Ra nge("A1")

      Now, it's on to the next hurdle in the learning curve. I'm sure I'll be back again. Thanks for helping out a newbie.

      Annie in Florida

      Comment

      Working...