+ Reply to Thread
Results 1 to 9 of 9

Trying to implement a Global Variable in VBA

  1. #1
    Registered User
    Join Date
    07-17-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    4

    Trying to implement a Global Variable in VBA

    Hello,

    Currently I have a submit button macro that moves a selected range of cells from one sheet to a protected sheet of compiled data. I also have an undo button macro that deletes the last submission from the protected sheet.

    I only want the undo macro to be able to run once before the submit macro is run again. Is there a way to implement a global variable such that the undo macro only runs if the variable is on?

    The variable would be set to off at the end of the undo macro and turned on after the submit macro.

    How can this be done? Any help would be greatly appreciated.

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Trying to implement a Global Variable in VBA

    You could declare a global boolean variable at the top of a code module or alternatively you could set the enabled property of the undo button to false, only enabling it when the submit macro has been run
    Elegant Simplicity............. Not Always

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,222

    Re: Trying to implement a Global Variable in VBA

    Welcome to the Forum!

    I really like AndyLitch's second idea. This is very good design--you don't need to manage a separate variable and check its value, and the user can't possibly do something you don't want him to do.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Registered User
    Join Date
    07-17-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Trying to implement a Global Variable in VBA

    How do I edit the enabling property of the button?

  5. #5
    Registered User
    Join Date
    07-17-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Trying to implement a Global Variable in VBA

    How do I edit the enabling property of the button?

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,222

    Re: Trying to implement a Global Variable in VBA

    If the name of the button is MyButton then

    Please Login or Register  to view this content.
    This also works with just about any control.

  7. #7
    Registered User
    Join Date
    07-17-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Trying to implement a Global Variable in VBA

    So I have two buttons each with their own macro --> Button1 (submit) and Button2 (undo)

    Within the code for Button1, I have:
    Button2.Enabled = True

    and within the code for Button2 I have:
    If Button2.Enabled = False Then Exit Sub

    and also at the end of the code

    Button2.Enabled = False


    From what I understood, this is what you told me to do however it still is not working...I get error messages when I run either the submit macro or the undo macro.

    Do you know why?

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,222

    Re: Trying to implement a Global Variable in VBA

    When asking for help regarding error messages it's always a good idea to include the error message, plus note which line of code is causing the error.

    What you wrote here looks mostly correct, although this code is unnecessary:
    Quote Originally Posted by adam2742 View Post
    and within the code for Button2 I have:
    If Button2.Enabled = False Then Exit Sub
    If Button2 is not enabled, the user can't press it and this code will never execute. Unnecessary, but it won't cause a problem.

    I would suggest you post all of your code, or better yet, attach your workbook.

    Also, there is a rule for posting code:

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

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

    Re: Trying to implement a Global Variable in VBA

    perhaps you have Forms buttons rather than activex ones
    Josie

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

+ 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. Replies: 3
    Last Post: 09-06-2005, 12:05 PM
  3. Replies: 0
    Last Post: 09-06-2005, 10:05 AM
  4. Replies: 2
    Last Post: 09-06-2005, 06:05 AM
  5. Replies: 0
    Last Post: 09-06-2005, 03:05 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