+ Reply to Thread
Results 1 to 8 of 8

Using Global variable in module

  1. #1
    Forum Contributor
    Join Date
    06-27-2013
    Location
    Nebraska
    MS-Off Ver
    Office 365
    Posts
    115

    Using Global variable in module

    Hello,

    I have a workbook that I am using to track costs throughout a project. This is used by multiple people and is normally protected once an initial cost estimate is determined. I have added an "Insert Revision" button that will allow a user to add a revision without overwriting any of the existing data. When inserting any revision they are prompted to enter their initials into an input box. I am wanting to prompt the user to enter their initials when they first open the workbook so they do not have to enter it for every revision they do. Once they enter their initials they would be used in the InsertRevision module and in the Workbook BeforeSave automatically generated email. I was trying to set this up with the Workbook_Open() macro. Is this the right way to go about it or do I need to do this with its own module?

    Thank you!
    Attached Files Attached Files

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Using Global variable in module

    There is no issue doing it in the workbook open code, BUT if you dim a variable inside a sub, then it stops existing once the sub is done... put it at the very top of the workbook module:

    Please Login or Register  to view this content.
    Also, a type 8 inputbox requires the entry to be a cell reference, while I believe you want text, so I changed it to type 2

    The word Public makes the variable available to all modules, while Private would make it available only to subs within the module it is declared in.

    Also, we use SET only for objects, so I removed that word from your code.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Contributor
    Join Date
    06-27-2013
    Location
    Nebraska
    MS-Off Ver
    Office 365
    Posts
    115

    Re: Using Global variable in module

    Arkadi,

    Thank you for the quick reply. I made the changes you suggested and was able to get the input box to pop up correctly when the document opens. I am trying to use the Enterinitials value in the below module but when I run InsertRevision it only shows the date in the column G. What do I need to do to get it to read Enterinitials from the workbook?

    Please Login or Register  to view this content.

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Using Global variable in module

    get rid of
    Please Login or Register  to view this content.
    It has been declared as public and exists for other modules, don't dim it anywhere else

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Using Global variable in module

    Global variables are set outside of a sub, at the top of any module. Once declared there, you can read or write to them in any sub (keep in mind what I said about Private/Public before). You should never Dim them again in any other code.

  6. #6
    Forum Contributor
    Join Date
    06-27-2013
    Location
    Nebraska
    MS-Off Ver
    Office 365
    Posts
    115

    Re: Using Global variable in module

    I am still not getting the initials to show up in column G when running the Insert Revision sub. I have tried changing Workbook_Open to a Public sub and that did not make any difference. I have attached the updated Test file as well.

    This the code for when the workbook opens
    Please Login or Register  to view this content.
    This is what I have for the Insert_Revision Sub
    Please Login or Register  to view this content.
    Attached Files Attached Files

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Using Global variable in module

    My bad, it would seem this line:

    Please Login or Register  to view this content.
    Needs to move from ThisWorkbook, to the top of one of your regular modules (should not matter which one, I tested by putting it in Module1.

    Leave the workbook open event as it is, that part is not a problem.

    Sorry, I had never tried putting it in ThisWorkbook but just assumed it would work, I was wrong and should have checked.
    Last edited by Arkadi; 03-23-2018 at 02:14 PM.

  8. #8
    Forum Contributor
    Join Date
    06-27-2013
    Location
    Nebraska
    MS-Off Ver
    Office 365
    Posts
    115

    Re: Using Global variable in module

    That worked! Thank you Arkadi.

+ 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 constant across whole module
    By malcmail in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2017, 10:29 AM
  2. Global Variable in a module
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2015, 10:08 AM
  3. [SOLVED] Global Constant Password ThisWorkbook Module
    By Delvesy888 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-21-2014, 06:14 AM
  4. How to Declare a Global Variable for a Module? Get the Border Color too!
    By e4excel in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 09-19-2011, 03:37 AM
  5. Global Variables being used in a spreadsheet module
    By ChemistB in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-04-2011, 12:37 PM
  6. Accessing Global Variables defined in ThisWorkbook Module
    By smz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-02-2010, 08:26 PM
  7. [SOLVED] Global (Module) Variable Problem
    By Allen Geddes in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-21-2005, 11:40 AM

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