+ Reply to Thread
Results 1 to 5 of 5

Global Variable

Hybrid View

  1. #1
    Registered User
    Join Date
    12-04-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    43

    Angry Global Variable

    I have created an application which is used for processing data for many years. At the start up of the application, I ask the user to select a year from a userform combobox. I want this value to be treated as global string variable. I defined it in the following manner
    Option Explicit
    Public glbYr As String
    Public Sub CommandButton2_Click()
    glbYr = ComboBox1.Value
    If glbYr <> "2012-13" And glbYr <> "2013-14" And glbYr <> "2014-15" Then
    MsgBox "Please Select Appropriate Period for Assessment."
    Call UserForm_Activate
    End If
    SelectYr.Hide 'This is user Form
    End Sub
    Then in a different module I was expecting that I would get the value of glbYr as received from this module. But I am getting null value.
    Please suggest..

    Sincerely
    Vivekchha

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Global Variable

    1) where is the above code placed ? It's wise (if not required?) to declare public variables in standard module.
    2) is really combobox1 value one of the expected values (does msgbox show?) if not (msgbox shows) you do not have the option to re-assign proper value - just do all activation-related tasks and close form
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    12-04-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: Global Variable

    Thanks. I got the mistake. The above codes are in a form module and not in standard module.

    Regards.

    Vivekchha

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,321

    Re: Global Variable

    Hi Vivekchha

    The topic of Scope of a variable is covered very well in:
    http://www.cpearson.com/excel/Scope.aspx

    I think Kaper is correct to ask where you declared the global variable.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Global Variable

    Put the declaration for the global variable in a standard module, the rest of the code should stay in the userform module.
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Global variable declared in Userform. Variable value is not sticking
    By timtim89 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-25-2012, 08:50 AM
  2. Replies: 2
    Last Post: 09-06-2005, 06:05 AM
  3. Replies: 0
    Last Post: 09-06-2005, 01:05 AM
  4. Replies: 0
    Last Post: 09-06-2005, 12:05 AM
  5. Replies: 0
    Last Post: 09-05-2005, 11:05 PM

Tags for this Thread

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