+ Reply to Thread
Results 1 to 15 of 15

Global variables?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-07-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    274

    Global variables?

    Hello,

    I would like that when my spread sheet opens, I set a constant global variable so that when the rest of the VBA code runs, it can access it from anywhere

    Here below is in the "ThisWorkbook" file whcih should execute right awaya as the excel file opens


    Public NoOfParts As Integer
    
    
    Sub setup()
    Dim PARTS As Integer
    
    PARTS = 9
    NoOfParts = PARTS - 1
    
    End Sub

    And here is some VBA code I execute via a button which should see the "NoOfParts" variable. But it doesn't????


    
    Sub ClrFilter()
    Dim unmergeTheCells As Integer
    Dim MyStr As String
    
    unmergeTheCells = ((NoOfParts + 1) * 5) + 5                ' <<<<<<< NoOfParts is EMPTY?????
    MyStr = "" & "B6" & ":E" & CStr(unmergeTheCells) & ""
    
    
    ' ....

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,914

    Re: Global variables?

    In a separate standard module add this line:
    Public PARTS As Integer
    You can then get rid of this line in your current code:
    Dim PARTS As Integer
    BSB

  3. #3
    Forum Contributor
    Join Date
    03-07-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    274
    Quote Originally Posted by BadlySpelledBuoy View Post
    In a separate standard module add this line:
    Public PARTS As Integer
    You can then get rid of this line in your current code:
    Dim PARTS As Integer
    BSB
    Yes but then how do I set parts minus 1 .... VBA doesn’t accept
    Code Im public space....

  4. #4
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Global variables?

    Hi all. @RustyNail, you might also consider moving your sub procedures to (the same?) standard module. Sheet and Workbook modules are traditionally reserved for event procedures and Class-related stuff.
    http://www.cpearson.com/excel/codemods.htm
    Clicking the Add Reputation star below helpful posts is a great way to show your appreciation.
    Please mark your threads as SOLVED upon conclusion (Thread Tools above Post # 1). - Lee

  5. #5
    Forum Contributor
    Join Date
    03-07-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    274
    Quote Originally Posted by leelnich View Post
    Hi all. @RustyNail, you might also consider moving your sub procedures to (the same?) standard module. Sheet and Workbook modules are traditionally reserved for event procedures and Class-related stuff.
    http://www.cpearson.com/excel/codemods.htm

    You mean move my setup sub in the same file as my other code...
    if so, how do I get it to execute as soon as my workbook
    Opens???

  6. #6
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Global variables?

    If you want that code to execute when the workbook opens, you must either put it in a Workbook_Open event procedure (which DOES belong in the ThisWorkbook module), or call it from same.
    Private Sub Workbook_Open()
        Dim PARTS As Integer
        PARTS = 9
        NoOfParts = PARTS - 1
    End Sub
    ...OR:
    Private Sub Workbook_Open()
        setup
    End Sub
    BTW, there's no logical reason to define PARTS at the procedure level in this scenario unless you're calculating its value in multiple steps. If you need to retain its value in memory, declare it at the module level.
    Last edited by leelnich; 02-28-2018 at 02:57 PM.

  7. #7
    Forum Contributor
    Join Date
    03-07-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    274

    Re: Global variables?

    Hello leelnich,

    I do your first one, but when VBA code runs in another sheet, NoOfParts is empty when its supposed to equal to 8 ????

    here's what I do:


    '================================ In This Workbook
    Public NoOfParts As Integer
    
    Private Sub Workbook_Open()
    Dim PARTS As Integer
    
    PARTS = 9
    NoOfParts = PARTS - 1
    
    'msgbox "I display on open"   ' <<<<<<<<< Message box pops up on Open
    
    End Sub
    
    '================================ A module in another sheet 
    
    Sub ClrFilter()
    Dim unmergeTheCells As Integer
    Dim MyStr As String
    
    unmergeTheCells = ((NoOfParts + 1) * 5) + 5     ' <<<<<<< NoOfParts is Empty?????
    MyStr = "" & "B6" & ":E" & CStr(unmergeTheCells) & ""
    
    
    '....
    
    End sub







    thanks for your reply
    Last edited by RustyNail; 02-28-2018 at 07:34 PM.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Global variables?

    Public variables MUST be declared in a standard code module, not in a class module (ThisWorkbook, a sheet module, or a custom class).
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Global variables?

    This in the ThisWorkbook Object module:
    Private Sub Workbook_Open()
    
    NoOfParts = 8
    
    End Sub
    ...and the rest, especially the variable declaration, in a standard (BAS) module:
    Public NoOfParts As Integer   '<<< This in a standard module.
    
    Sub ClrFilter()
    Dim unmergeTheCells As Integer
    Dim MyStr As String
    
    unmergeTheCells = ((NoOfParts + 1) * 5) + 5     ' <<<<<<< NoOfParts is Empty?????
    MyStr = "" & "B6" & ":E" & CStr(unmergeTheCells) & ""
    
    
    '....
    
    End Sub
    To create a new BAS module, use the Insert menu, or press ALT+I, ALT+M.
    Check this link for a discussion of VBA variable Scope:
    http://www.cpearson.com/excel/Scope.aspx
    Last edited by leelnich; 02-28-2018 at 08:15 PM.

  10. #10
    Forum Contributor
    Join Date
    03-07-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    274

    Re: Global variables?

    Ok thanks leelnich 👍

  11. #11
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Global variables?

    You're welcome! If concluded, please mark your thread as SOLVED (Thread Tools above post #1). Regards – Lee

  12. #12
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Global variables?

    Why use a variable at all if you are going to set it to a fixed value in the Workbook_Open event? Simply put
    Public Const NoOfParts As Integer = 8
    at the top of a normal module and remove the Open event code.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  13. #13
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Global variables?

    Quote Originally Posted by xlnitwit View Post
    Why use a variable at all ...
    True IF the value isn't changed later... but why would you assume that?

  14. #14
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Global variables?

    I based the assumption on the fact the OP stated "I set a constant global variable"

  15. #15
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Global variables?

    OOPS!
    ...and quite a reasonable conclusion it is, sir!

+ 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. Global Variables
    By Slyone2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2010, 12:20 PM
  2. Global variables
    By Ed in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-06-2006, 11:05 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 Francis Brown in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-27-2005, 02:20 PM
  5. global Variables
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-25-2005, 03:10 PM
  6. global variables
    By Kooshesh@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-16-2005, 07:50 PM
  7. [SOLVED] Global Variables
    By Ernst Guckel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-07-2005, 07:06 PM

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