+ Reply to Thread
Results 1 to 5 of 5

Using a variable in a userform label

Hybrid View

BigDubbe Using a variable in a... 05-30-2008, 10:41 AM
dominicb Good afternoon BigDubbe ... 05-30-2008, 10:46 AM
BigDubbe I see what you are saying,... 05-30-2008, 11:07 AM
royUK If you are setting X in a... 05-30-2008, 11:15 AM
BigDubbe Thank you after a little... 05-30-2008, 11:25 AM
  1. #1
    Registered User
    Join Date
    08-02-2006
    Posts
    21

    Using a variable in a userform label

    So basically what I want to do is in the body of the macro I have set x = 1 and I want to show that answer in a userform as label1. How would I go about referring to that variable.

    I have tired:

    UserForm1.Label1.Caption = x (and that doesnt work)

    Any help would be awesome

    Thanks

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good afternoon BigDubbe

    Your syntax is quite correct (assuming that your your form is called UserForm1 and your label is calledLabel1). That, and the fact that no error is being generated probably makes me think that you are setting a value to x in one event driven sub, and calling it in another. Put this line at the very top of your userform code, before any of the subs and see if it works. This will make the variable x available to all subs in the userform module :
    Dim x as Long
    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Registered User
    Join Date
    08-02-2006
    Posts
    21
    I see what you are saying, but X is not being set in the userform. I have X being set in a different module. So I dont know how to refer to variables in that module.

    I was messing around but couldnt get anything to work example of what I was trying:


    UserForm1.Label1.Caption = Module1.X (Didnt work)

    I hope this makes the question more clear.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    If you are setting X in a module other than the userform it must be a Global Variable. In the module

    Option Explicit
    Public X As Long
    
    Sub setX()
    X = 1
    UserForm1.Show
    End Sub
    In the UserForm
    Option Explicit
    
    
    Private Sub UserForm_Initialize()
    On Error Resume Next
    Me.Label1.Caption = X
    On Error GoTo 0
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    08-02-2006
    Posts
    21
    Thank you after a little tweaking it is working.

+ 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