+ Reply to Thread
Results 1 to 14 of 14

Changing the Caption of CommandButtons

  1. #1
    Registered User
    Join Date
    03-05-2013
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    14

    Changing the Caption of CommandButtons

    So I have created this UserForm and a spreadsheet which generates 9 unique options, one of which matches the Japanese for my daughter to improve her Japanese.

    Userform image.JPG

    I need the CommandButtons to change automatically. I have Named the nine options (CommandButton1 to CommandButton9) but I have not seen a method to change them to match the spreadsheet's choices.

    I've seen similar posts and solutions from the past, but not quite what I am after. Any ideas?

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    Setting the caption of a command button to a value from a worksheet is quite straightforward.

    For example, this will set the caption of CommandButton1 to the value in A2 on Sheet2.
    Please Login or Register  to view this content.
    What do you need the caption(s) to change to and when do you need them to change?
    If posting code please use code tags, see here.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Changing the Caption of CommandButtons

    Changing a CommandButton's caption is easy, you can build something like this right into each CommandButton?_Click event:

    Please Login or Register  to view this content.
    You can use whatever method works best for you to change dog to a different string, you mentioned sheet names, perhaps.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    03-05-2013
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    14

    Re: Changing the Caption of CommandButtons

    Thank you for your responses.

    Norie; where do I slide in that piece of code. I thought and tried unsuccessfully to put in Declarations. Perhaps Initialize is a better option - nope, no luck there either.

    Jbeaucaire; this changes the button to the name I want only after I click it - which is something I will utilise in the future. I need the button caption to be
    Please Login or Register  to view this content.
    or better yet, the Name Option1.
    Last edited by OberonViking; 03-06-2013 at 05:04 AM. Reason: spelling error (typo)

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Changing the Caption of CommandButtons

    I highlighted "dog" in blue in the original example to draw attention, you can substitute that with any source text you want.

  6. #6
    Registered User
    Join Date
    03-05-2013
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    14

    Re: Changing the Caption of CommandButtons

    Quote Originally Posted by JBeaucaire View Post
    I highlighted "dog" in blue in the original example to draw attention, you can substitute that with any source text you want.
    Thank you, yes I understood that part. What I am missing is how to have the CommandButton.Caption equal to a cell value before clicking on it so that as the cell value changes so does the display on the command button. In this way it will work as a series of multiple choice questions.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Changing the Caption of CommandButtons

    You'll have to set the caption correctly once manually the first time.

    After that, Norie's shown you how to change it after that.

  8. #8
    Registered User
    Join Date
    03-05-2013
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    14

    Re: Changing the Caption of CommandButtons

    Quote Originally Posted by JBeaucaire View Post
    You'll have to set the caption correctly once manually the first time.

    After that, Norie's shown you how to change it after that.
    I can't have it display a cell value (which is randomised) from the beginning?
    Can I get it to run Commandbutton1.Caption = "dog" as it loads the first time?

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Changing the Caption of CommandButtons

    Oh, this button is part of a FORM? You can set the starting value of the CommandButton1 in the form's INITIALIZE macro. Use my method or Norie's to set the caption as you see fit.

  10. #10
    Registered User
    Join Date
    03-05-2013
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    14

    Re: Changing the Caption of CommandButtons

    Ahh, I will expore that when I get home. I thought there was something like that but did not know the name.

  11. #11
    Registered User
    Join Date
    03-05-2013
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    14

    Re: Changing the Caption of CommandButtons

    Okay, so where am I going wrong?

    Please Login or Register  to view this content.

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Changing the Caption of CommandButtons

    To reference a sheet the options are:

    Please Login or Register  to view this content.
    Each one means something different. If you're trying to reference the sheet by the sheet name, then name goes in quotes, like the third example.

  13. #13
    Registered User
    Join Date
    03-05-2013
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    14

    Re: Changing the Caption of CommandButtons

    Excellent - I had this working as I wanted when it crashed and I lost an hour or two of work.
    Trying to think positively, it will help me to remember it in the future, having to type it all out again.

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Changing the Caption of CommandButtons

    Hehe, happened to me many times, however my Excel autosaves every 5 minutes so I am always able to recover files after a crash. CTRL-S is your friend.

    If that takes care of your original query, please select Thread Tools from the menu above and mark the thread as solved. Thanks.

+ 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