+ Reply to Thread
Results 1 to 4 of 4

Enable a Macro only when there are Entries in Two Cells

  1. #1
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Enable a Macro only when there are Entries in Two Cells

    I have a button (Shape) which when clicked runs a macro. I'd like this to be disabled and greyed out until there are entries in two cells (see attached sample). When there are entries in both cells, the button's format should change and allow the macro to run when clicked.

    Is there any way to achieve this?

    Many thanks
    Thanks,
    HangMan

    You can say "Thank you!" by clicking Add Reputation below the post.
    Please, mark your thread [SOLVED] if you are happy with the solution.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Enable a Macro only when there are Entries in Two Cells

    Hi HangMan,

    I was able to do what you want by adding code to "Hello World" and putting code in the 'Sheet' Worksheet_Change() event.

    The 'Shape name' must be in the code , and the 'Shape name' MUST be unique. If the name is not unique, VBA will use the first Shape that has that name. In addition, the 'Hello World' Macro cannot service multiple shapes as written.

    My code assumes shape name 'Rounded Rectangle 1'.

    Modified Hello World Code:
    Please Login or Register  to view this content.
    Sheet Module Code:
    Please Login or Register  to view this content.
    -----------------
    A much simpler approach which I use in my own applications, is to Hide the Shape when you want to disable it. This requires NO MODIFICATIONS to the 'Hello World' Macro, and the 'Hello World' macro can service many shapes if needed.

    The Sheet module code for this would be:
    Please Login or Register  to view this content.
    Lewis

  3. #3
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Enable a Macro only when there are Entries in Two Cells

    I hadn't considered the idea of 'hiding' the button. I think that is potentially a much better approach as it means the user can't run the macro without populating the two cells (although in practice of course they could still run it manually, but I'm not worried by that).

    I applied your code to my main sheet and it seems to be working perfectly...

    Many thanks for your idea and help with this, it is very much appreciated...

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Enable a Macro only when there are Entries in Two Cells

    Hi,

    Thanks for the kind words and the rep points.

    I think that is potentially a much better approach as it means the user can't run the macro without populating the two cells (although in practice of course they could still run it manually, but I'm not worried by that).
    For future reference, code like the following will prevent the user running the Macro manually.
    Please Login or Register  to view this content.
    Lewis

+ 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. Replies: 1
    Last Post: 01-29-2015, 12:36 PM
  2. [SOLVED] Is it mandatory to keep the excel file as macro enable (if inside having macro) ?
    By nur2544 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-09-2014, 11:39 PM
  3. macro to open excel file with auto macro enable
    By jamewoong in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-14-2014, 10:44 AM
  4. Replies: 0
    Last Post: 08-06-2013, 08:21 AM
  5. Big challenge: protect multiple sheets AND enable outlining AND enable insert comment
    By visschercaravelle in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-23-2010, 11:46 AM
  6. Macro to automatically fill cells based on previous entries
    By jerinjan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2008, 08:13 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