+ Reply to Thread
Results 1 to 6 of 6

Help Writing Code for Command Button

  1. #1
    Registered User
    Join Date
    08-26-2019
    Location
    Houston, TX
    MS-Off Ver
    2016
    Posts
    8

    Help Writing Code for Command Button

    I'm am looking to understand what code needs to be entered to produce the following scenario. I'm sure it's suer simple, I am just uncertain myself.

    I'd like one command button that when pushed toggles the text in one cell between "YES" and "NO". Also, When pressed I'd like the text displayed on the command button itself to correspond with the populated cell to match ("YES" or "NO").

    Thanks!
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    03-28-2018
    Location
    Houston, TX
    MS-Off Ver
    Microsoft Office Professional Plus 2016
    Posts
    127

    Re: Help Writing Code for Command Button

    I've attached a workbook with what you described. A couple of notes if you want to do this yourself:

    1) I prefer using shapes to create buttons for running my macros instead of command buttons. There are so many quick formatting options available that make them look so much nicer than the "we're still in the 90s" gray command buttons. This method will work for either though, so do as you choose.
    2) If you click on a shape or textbox, or anything of that sort to put focus on it and then type a formula in the formula bar, it works just like a cell formula. With this, you can set the text on the button equal to the text in the cell you will be changing.
    3) I used a named range called "YesNo" for the cell that will be toggled.
    4) Here is the code that you will need to make this work (this code assumes the cell being changed is in the same tab as the button itself):
    Please Login or Register  to view this content.
    Let me know if you have any questions.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    08-26-2019
    Location
    Houston, TX
    MS-Off Ver
    2016
    Posts
    8

    Re: Help Writing Code for Command Button

    Quote Originally Posted by brant.wheeler View Post
    I've attached a workbook with what you described. A couple of notes if you want to do this yourself:

    1) I prefer using shapes to create buttons for running my macros instead of command buttons. There are so many quick formatting options available that make them look so much nicer than the "we're still in the 90s" gray command buttons. This method will work for either though, so do as you choose.
    2) If you click on a shape or textbox, or anything of that sort to put focus on it and then type a formula in the formula bar, it works just like a cell formula. With this, you can set the text on the button equal to the text in the cell you will be changing.
    3) I used a named range called "YesNo" for the cell that will be toggled.
    4) Here is the code that you will need to make this work (this code assumes the cell being changed is in the same tab as the button itself):
    Please Login or Register  to view this content.
    Let me know if you have any questions.
    Thank you Brant! I've got the functionality I'm looking for to work (the part where the desired cell toggles back and forth from YES to NO) but I can't seem to get the text on the command button box to correlate. Can this maybe be done through the properties?

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Help Writing Code for Command Button

    Quote Originally Posted by brant.wheeler View Post
    ou will need to make this work (this code assumes the cell being changed is in the same tab as the button itself):
    Please Login or Register  to view this content.
    Since you are using a Defined Name for the toggle cell, your macro can be simplified quite a bit...
    Please Login or Register  to view this content.

  5. #5
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,302

    Re: Help Writing Code for Command Button

    Super simple, flip-flop the command button actions.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    08-26-2019
    Location
    Houston, TX
    MS-Off Ver
    2016
    Posts
    8

    Re: Help Writing Code for Command Button

    Quote Originally Posted by Rick Rothstein View Post
    Since you are using a Defined Name for the toggle cell, your macro can be simplified quite a bit...
    Please Login or Register  to view this content.
    Quote Originally Posted by torachan View Post
    Super simple, flip-flop the command button actions.
    Please Login or Register  to view this content.
    Thank you guys!! I was able to get it to work!

+ 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. VBA code to create command button. Print all sheets then delete command button.
    By Declamatory in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-15-2015, 05:18 PM
  2. [SOLVED] Run Command Prompt Icacls code in VBA Command Button on Userform
    By luismalave in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-19-2015, 11:40 PM
  3. Create a command button with code with a command button
    By jakara in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-29-2013, 01:28 PM
  4. Format or command options not show after the . when writing code
    By pinball4530 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-17-2012, 08:55 AM
  5. Code for a master command button to change the backcolor of multiple command buttons?
    By panttherm5 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2012, 10:11 PM
  6. [SOLVED] vba code, command bar, command bar button, one button works but not two
    By amazingg64 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2012, 09:55 AM
  7. Trying to Give New Command Button Code from Another Sheet's Code
    By MegaRush in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2012, 09:43 AM

Tags for this Thread

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