+ Reply to Thread
Results 1 to 11 of 11

Defining Global Variable from form to sheet

  1. #1
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Defining Global Variable from form to sheet

    Here's what supposed to happen:
    From a button in worksheet with code in the worksheet, I call a user form "Password"
    I input a password which it saves as "PWord" and it checks to see if it's good (in user form code) and unlocks the sheet if it is.
    Then hides the form
    The code in worksheet then does some stuff and locks the sheet using PWord
    However, seems PWord has become "" and I'm left with an unprotected worksheet.

    I've tried putting
    "Dim PWord as string" outside the Form code, outside the worksheet code but doesn't seem to matter.
    Any thoughts on what I'm doing wrong?
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Defining Global Variable from form to sheet

    Try;

    Please Login or Register  to view this content.
    I alway put all my Globals at the top, right under Option Explicit....HTH
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Defining Global Variable from form to sheet

    you need to have
    Please Login or Register  to view this content.
    in a normal module and remove all other declarations of the variable. or make it a property of the worksheet and have the form set that property
    Last edited by JosephP; 06-15-2013 at 02:31 AM. Reason: typo
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Defining Global Variable from form to sheet

    Thank you I will try that.

  5. #5
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Defining Global Variable from form to sheet

    Bump....just curious if that fixed your issue.....inquiring minds want to know...thanks

  6. #6
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Defining Global Variable from form to sheet

    Hi Judge,
    Sorry, was working on other things and am just getting back to this. It did not work. I'm attaching a stripped down version. Maybe I'm putting the "Public" statement in the wrong place. The password is "thisword"
    Clicking on the green button "Unhide Details" should (and does) pull up the password form to enter a password but this password is not being carried back to the main routine.
    Attached Files Attached Files

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Defining Global Variable from form to sheet

    Insert a new standard module (Insert>Module).

    Declare PWord in the new module.

    Also, remove all other declarations of PWord.
    If posting code please use code tags, see here.

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Defining Global Variable from form to sheet

    Hi ChemistB

    You have Public PWord As String in the Sheet Module (Login) and in ThisWorkbook Module...get them out of these two places. The put it is a Standard Module.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  9. #9
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Defining Global Variable from form to sheet

    yup....everybody seems to be in agreement....bad thing on my part, I assumed you had a standard module and I should have asked for an example....

  10. #10
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Defining Global Variable from form to sheet

    Yep, this worked. Good to know. Thanks to all!

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Defining Global Variable from form to sheet

    Hi ChemistB

    If you'd be so kind, please mark your Thread as "Solved"...

+ 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