+ Reply to Thread
Results 1 to 10 of 10

Setting a Global Variable

  1. #1
    Forum Contributor
    Join Date
    05-24-2014
    MS-Off Ver
    Microsoft Office 2013
    Posts
    113

    Setting a Global Variable

    So I've read quite a few things on global variables but none of it has made sense to me. I want to set a variable that works in all my modules to set the name of my printer for each macro. Here is the code that I have within each macro and I just want it in one place because it seems like whenever I restart my computer or have to unhook the printer it changes the Ne##. If anyone knows how to prevent that from happening that would be great as well. Thanks for any help.

    Please Login or Register  to view this content.
    Last edited by manofcheese; 06-17-2014 at 10:44 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Setting a Global Variable

    If you only want to have to change it in one place you can use a constant rather than a variable:
    Please Login or Register  to view this content.
    You can also use a function to return the port number (the NExx bit): http://www.excelforum.com/tips-and-t...excel-vba.html
    Last edited by romperstomper; 06-12-2014 at 03:01 AM. Reason: Add link
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Contributor
    Join Date
    05-24-2014
    MS-Off Ver
    Microsoft Office 2013
    Posts
    113

    Re: Setting a Global Variable

    So to use them both would I have to change anything in your code from the other thread? or would it just be like this.
    Please Login or Register  to view this content.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Setting a Global Variable

    You could use a combination of the two:

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Setting a Global Variable

    Maybe:
    Please Login or Register  to view this content.
    would be a little more versatile. As an aside I'd rather have a public class for constants since I'm lazy and it gives me intellisense

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Setting a Global Variable

    That's true, but then all the current code has to be changed, and I'm very lazy.

  7. #7
    Forum Contributor
    Join Date
    05-24-2014
    MS-Off Ver
    Microsoft Office 2013
    Posts
    113

    Re: Setting a Global Variable

    Thanks Romerstomper. It worked like a charm. I didn't have a chance to try Kyle123's solution but I bet it would work too. I am just bad at coding and don't know what changes it makes.

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Setting a Global Variable

    If you're feeling really really lazy :
    Please Login or Register  to view this content.
    Zero code change - but not advisable

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Setting a Global Variable

    The original code used printer_name as the variable though.

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Setting a Global Variable

    Ah yes, thought I was being clever

+ 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. Setting Global Variable from a UserForm
    By agwright in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-03-2009, 09:59 AM
  3. Setting a Global Variable
    By ChemistB in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-29-2008, 10:51 AM
  4. [SOLVED] Setting form caption based on global variable
    By JNW in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-20-2006, 05:50 PM
  5. Setting a Custom Class as a Global variable
    By Xiazer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-31-2006, 06:55 AM

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