+ Reply to Thread
Results 1 to 6 of 6

Global Variables

  1. #1
    Registered User
    Join Date
    09-08-2014
    Location
    -
    MS-Off Ver
    -
    Posts
    68

    Global Variables

    Hey! I'm aware that this pops up seven hundred billion times a day, but I can't seem to get it working whatsoever.
    My users have to use a sequence of userforms in my VBA project. In the first one, they choose some previously imported data to base the rest of the work on.
    This imported data is a worksheet in the workbook, with name wsdata
    Based on that, it creates a new worksheet, with name wsinvoice.
    However, once I get to the next userform, wsinvoice and wsdata are cleared so they have no value. I could make it so wsinvoice is always added at the end of the workbook thus making wsinvoice always Sheets(Worksheets.Count), but that doesn't solve the wsdata.

    Theoretically, I should just be able to do the following in the first userform:

    Please Login or Register  to view this content.
    No luck though. wsdata and wsinvoice are useless outside of userform1...halp? <3

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

    Re: Global Variables

    This
    Please Login or Register  to view this content.
    Needs to be in a module, not a userform

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

    Re: Global Variables

    'Global' is a relic from older versions of VB - you should use Public.

    Anyway, if you declare a Public variable in a class module (which a userform is) you have to refer to it as a member of that class, so your other code would have to refer to Userform1.wsdata$ and Userform1.wsinvoice$

    Alternatively, you can move the public declarations to a normal module, or pass the variables directly from form to form as properties, or use one form with a multipage control rather than multiple forms. Hard to say which is best for your situation on current information.
    Everyone who confuses correlation and causation ends up dead.

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

    Re: Global Variables

    And Public variables in modules are evil

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

    Re: Global Variables

    They're not evil. People who use them are evil.

  6. #6
    Registered User
    Join Date
    09-08-2014
    Location
    -
    MS-Off Ver
    -
    Posts
    68

    Re: Global Variables

    XD XD XD This forum is great. You've made Lady Marmalade a happy man XD
    Given that I'm now classed as being evil, I best cackle my way back to my attempt at VBA Thanks guys! :D

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