+ Reply to Thread
Results 1 to 15 of 15

How to capture command button label text?

  1. #1
    Registered User
    Join Date
    11-06-2012
    Location
    northern california
    MS-Off Ver
    Excel 2002
    Posts
    10

    How to capture command button label text?

    Can anyone provide a code snippet for capturing the text on a command button? I need to paste the text to a cell. Thanks in advance!

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

    Re: How to capture command button label text?

    Something like this perhaps.
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: How to capture command button label text?

    Assuming the command buttion is named CommandButton1. If you need to paste it in a cell it would just be:

    Please Login or Register  to view this content.
    edit: got beat

  4. #4
    Registered User
    Join Date
    11-06-2012
    Location
    northern california
    MS-Off Ver
    Excel 2002
    Posts
    10

    Re: How to capture command button label text?

    [QUOTE=stnkynts;2998700]Assuming the command buttion is named CommandButton1. If you need to paste it in a cell it would just be:

    Please Login or Register  to view this content.
    Thanks! Do you know of a way to do this without knowing the button's name? I'm trying to develop a single macro to work on hundreds of buttons (I also don't want hundreds of macros). For instance, is there a way for the macro to know which button it is by the fact it was just pushed?

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

    Re: How to capture command button label text?

    cadnauseam

    I think we need some more information.

    Where are the 100s of command buttons located?

    Why do you want to capture their caption?

  6. #6
    Registered User
    Join Date
    11-06-2012
    Location
    northern california
    MS-Off Ver
    Excel 2002
    Posts
    10

    Re: How to capture command button label text?

    Quote Originally Posted by Norie View Post
    cadnauseam

    I think we need some more information.

    Where are the 100s of command buttons located?

    Why do you want to capture their caption?

    I have a sheet with hundreds of parts catalogued, with descriptions, part numbers, and an image for each. Under each image is a cell with the name of the part. And under that cell is one with a command button. I want the user to click on this button, and have the name of the part copied to Sheet 1, cell A1. An external program will read this cell. I could hardcode the part name into each macro, but I was trying to avoid having so many unique macros.

    I looked at a way to have the macro read the part name, but couldn't make that work. See:
    http://www.excelforum.com/excel-prog...23#post2997423

  7. #7
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: How to capture command button label text?

    It will take more time to create some fancy macro, if at all possible, than it would take to put in one line of code for 100 command buttons. Just copy/paste and change the command button name.

  8. #8
    Registered User
    Join Date
    11-06-2012
    Location
    northern california
    MS-Off Ver
    Excel 2002
    Posts
    10

    Re: How to capture command button label text?

    Quote Originally Posted by stnkynts View Post
    It will take more time to create some fancy macro, if at all possible, than it would take to put in one line of code for 100 command buttons. Just copy/paste and change the command button name.
    Thanks for responding. You're right about the time factor, but it's worth it to me to investigate a bit more for the right solution because the scope of this project is going to grow. What is a hundred now could easily become a thousand. Although at that point I will have to look at breaking it up into different pieces based on categories, and design some kind of front end for it.
    Last edited by cadnauseum; 11-07-2012 at 05:52 PM. Reason: typo

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

    Re: How to capture command button label text?

    cadnauseam

    Can you attach a small sample workbook to demonstrate what you want to do?

    There might be other approachs that can be taken to achieve it.

  10. #10
    Registered User
    Join Date
    11-06-2012
    Location
    northern california
    MS-Off Ver
    Excel 2002
    Posts
    10

    Re: How to capture command button label text?

    Quote Originally Posted by Norie View Post
    cadnauseam

    Can you attach a small sample workbook to demonstrate what you want to do?

    There might be other approachs that can be taken to achieve it.
    I've attached a sample workbook that illustrates what I'm trying to accomplish. The macro GetImageName works, but only if the command button's cell is the active one.
    Attached Files Attached Files

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

    Re: How to capture command button label text?

    This code will get the corresponding image name when the a button is pressed.

    It kind of relies on your current layout with the button in the cell below the image name.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  12. #12
    Registered User
    Join Date
    11-06-2012
    Location
    northern california
    MS-Off Ver
    Excel 2002
    Posts
    10

    Re: How to capture command button label text?

    Can the string be written directly to Sheet1, Cell A1 , without popping up the message box?

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

    Re: How to capture command button label text?

    You can do anything you want with the string - the messagebox is just to show the code works.

    Please Login or Register  to view this content.

  14. #14
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: How to capture command button label text?

    @ Norie,


    Nice one!
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  15. #15
    Registered User
    Join Date
    11-06-2012
    Location
    northern california
    MS-Off Ver
    Excel 2002
    Posts
    10

    Re: How to capture command button label text?

    Quote Originally Posted by Norie View Post
    You can do anything you want with the string - the messagebox is just to show the code works.

    Please Login or Register  to view this content.
    Norie - THANK YOU !!!
    I replaced Sheet1 in the btn assignment with ActiveSheet, since there will be multiple sheets. It works just as I expect it to.

    Thanks again, you rock!

+ 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