+ Reply to Thread
Results 1 to 6 of 6

How to use global variables in VBA?

Hybrid View

  1. #1
    Registered User
    Join Date
    11-13-2014
    Location
    Portugal
    MS-Off Ver
    2013
    Posts
    3

    Question How to use global variables in VBA?

    Hello, trying to implement a global variable iTable that I can increment/decrement and use its value anytime needed.

    1. globalvariables module:

    Global iTable As Integer
    2. One of my subs that change its value:

    Private Sub txtProduct1_Enter()
        iTable = 3
    End Sub
    3. Sub where I call it with a Select Case

    Private Sub populateTxtOnEnter(ByVal copy As String, ByVal iTable As Integer)
        Select Case iTable
        Case 1
            MsgBox "ERROR"
            txtClient.Value = copy
            textTicket.SetFocus
        Case 2
            textTicket.Value = copy
            txtProduct1.SetFocus
        Case 3
            txtProduct1.Value = copy
            'txtSize1.SetFocus
        Case 4
            txtSize1.Value = copy
            txtQuantity1.SetFocus
        'Case 5
        'Case 6
        'Case 7
        Case 8
            txtProduct2.Value = copy
            txtSize2.SetFocus
        Case 9
            txtSize2.Value = copy
            txtQuantity2.SetFocus
        'Case 10
    
        End Select
        
    End Sub
    Now, altough I enter txtProduct1 TextBox, and my iTable is changed to 3, when the code reachs the Select Case, it always goes Case 1 and displays "ERROR" MsgBox that I created. I ask you why?

    I tried a MsgBox on txtProduct1_Enter and it give me iTable = 3.

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: How to use global variables in VBA?

    Hi,

    Because you set the second argument name of sub populateTxtOnEnter() also as iTable.

    When you call the sub populateTxtOnEnter(argument1, argument2), you pass some values, and you must be passing value 1 as second argument, this value will override global variable.

    Regards
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Registered User
    Join Date
    11-13-2014
    Location
    Portugal
    MS-Off Ver
    2013
    Posts
    3

    Re: How to use global variables in VBA?

    Private Sub populateTxtOnEnter(ByVal copy As String, ByVal id As Integer)
    Select Case id
    ...

    Made this change and it still always goes back to 1. I remember that in java the arguments were local, meaning that they could have any name you want, it wouldnt override. In VBA all variables/arguments variables must be unique?

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: How to use global variables in VBA?

    I don't see all your code, but my feeling tells me that you don't need second argument at all.

    So your code would be like :

    Private Sub populateTxtOnEnter(ByVal copy As String)
        Select Case iTable
        Case 1
            MsgBox "ERROR"
            txtClient.Value = copy
            textTicket.SetFocus
        Case 2
            textTicket.Value = copy
            txtProduct1.SetFocus
        Case 3
            txtProduct1.Value = copy
            'txtSize1.SetFocus
        Case 4
            txtSize1.Value = copy
            txtQuantity1.SetFocus
        'Case 5
        'Case 6
        'Case 7
        Case 8
            txtProduct2.Value = copy
            txtSize2.SetFocus
        Case 9
            txtSize2.Value = copy
            txtQuantity2.SetFocus
        'Case 10
    
        End Select
        
    End Sub

    Quote Originally Posted by Satsulima View Post
    I remember that in java the arguments were local, meaning that they could have any name you want, it wouldnt override. In VBA all variables/arguments variables must be unique?
    Yes, same for VBA, but local variables is dominant over global variables. As soon as you exit the sub, the global vars will take place again.

  5. #5
    Registered User
    Join Date
    11-13-2014
    Location
    Portugal
    MS-Off Ver
    2013
    Posts
    3

    Re: How to use global variables in VBA?

    You are absolutely right. Im beginning to figure out my problem. Its all fine on the global variable. Its actually my 1109 lines code thats getting complex. Summarizing it, I did a bunch of MsgBox on my subroutines to track down iTable value.

    It appears to me that theres other sub calling txtClient_Enter that has iTable = 1 inside... I must now wonder what subs are related, wich ones call wich ones, etc. Any suggestion or approach for this?

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: How to use global variables in VBA?

    You can search for (Ctrl F) iTable, then insert a debugging line before the line of assignment of iTable.

    For ex :

    Sub MySub1()
    ....
    ....
    iTable = 1
    ....
    End Sub()

    Change the code to :
    Sub MySub1()
    ....
    ....
    Debug.Print "Called from MySub1"
    iTable = 1
    ....
    End Sub()

    You then can track the process by watching the immediate window.

+ 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 Variables
    By MarmaladeLover in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-29-2014, 12:10 PM
  2. Global Variables
    By Slyone2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2010, 12:20 PM
  3. keep the global variables
    By Mike Archer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-03-2006, 02:40 PM
  4. Global Variables
    By mattsvai in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-03-2006, 01:10 PM
  5. global Variables
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-25-2005, 03:10 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