+ Reply to Thread
Results 1 to 5 of 5

Setting Global Variable from a UserForm

  1. #1
    Registered User
    Join Date
    07-31-2009
    Location
    Darien, IL
    MS-Off Ver
    Excel 2003
    Posts
    4

    Setting Global Variable from a UserForm

    Hi everyone,

    I have a UserForm that runs when my excel project starts that prompts the user for two pieces of data: a username and a password (these are not for logging into the file itself, I need to use them to call web queries later). I want the UserForm to store both of these data in global variables, so that macros that are run in the future can refer to them and read their values. Unfortunately, I am very new to VBA and I cannot figure out what code I need and where exactly I need to put it. Here is my current code, where "authentication" is the name of the UserForm object, and the textboxes I use for entry are named "user" and "pass":

    In "This Workbook"
    Please Login or Register  to view this content.
    In "authentication"
    Please Login or Register  to view this content.
    Last edited by agwright; 08-14-2009 at 11:56 AM.

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Setting Global Variable from a UserForm

    Declare your variable at the top of the module sheet - before any code

    Use Public when declaring the variable to make it available to all macros with the Workbook
    Use Private when declaring the variable to make it available only to macros on that Module Sheet
    Place variable within the Macro to make it available to only that macro

    Please Login or Register  to view this content.
    Also suggest you have a look at
    http://www.cpearson.com/excel/DeclaringVariables.aspx
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    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: Setting Global Variable from a UserForm

    You need to declare public variables (variables that have scope across all modules in a project) in a code module, not an object (workbook, sheet, form, class) module.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    07-31-2009
    Location
    Darien, IL
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Setting Global Variable from a UserForm

    Thank you both! It's working now. Sorry I didn't get back to you sooner but I'm not in the office over the weekend.

  5. #5
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Setting Global Variable from a UserForm

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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