+ Reply to Thread
Results 1 to 33 of 33

How to obtain Object name

  1. #1
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    How to obtain Object name

    Hi to everybody,


    If a create an activex button and I put the caption "Run macro", the properties would be like this:
    (Name)=CommandButton1
    Caption=Run macro

    Then, somebody knows how to obtain the (Name) part using VBA code, I mean, to know if the button
    name is CommandButton1 or CommandButton20?

    Any help would be appreciated.

  2. #2
    Forum Contributor
    Join Date
    01-02-2007
    Location
    Australia NSW
    MS-Off Ver
    2013
    Posts
    494

    Re: How to obtain Object name

    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: How to obtain Object name

    Is this what you're after:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  4. #4
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    Hello coreytroy,

    Thank you for answer.

    But actually I have several buttons and I want to know how they are named.

    But you're using in your code "CommandButton1" that is the part I want to know, because the buttons could be named as
    CommandButton2, CommandButton7, CommandButton15 and CommandButton20 etc.

    I want to obtain the Name, not the Caption.

    I hope make sense.


    Thanks in advance.
    ------------------------------------------------------------

    Hello Trebor,

    Thank you, but I got Run-time error 13, type mismatch.

    I don't want the Caption that is visible over the button. I want the Name
    "CommanButtonX" of each of the activex buttons I have that is not visible.

    Thanks again
    Last edited by cgkmal; 02-27-2013 at 01:54 AM.

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: How to obtain Object name

    Thank you, but I got Run-time error 13, type mismatch.
    Because you stepped through or just ran the code, it needs to be assigned to and then run from the button.

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

    Re: How to obtain Object name

    application.caller doesn't work for activex controls. perhaps
    Please Login or Register  to view this content.
    Josie

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

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: How to obtain Object name

    application.caller doesn't work for activex controls
    It worked for me

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

    Re: How to obtain Object name

    I'd like to see that-can you post a workbook?

  9. #9
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    Hello Joseph,

    Thank you for your help!

    I've tried your code putting the output in column A of sheet Menu, but only print correctly
    the first button name (CommandButton18), but for the others only puts the Caption.

    Please take a look in the attachement.

    Thanks in advance.
    Attached Files Attached Files

  10. #10
    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 obtain Object name

    The name and the caption of Option Button 2, Option Button 3 etc are the same.
    If posting code please use code tags, see here.

  11. #11
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    Hello Norie,

    If you see properties of each button, you'll see that each button has different
    Name. The first is CommandButton18, the 2nd CommandButton1, the 3rd CommandButton2
    and so on.


    Best regards

  12. #12
    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 obtain Object name

    Oops, I was looking in the wrong place.

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

    Re: How to obtain Object name

    I'm not sure you can access the names. usually the names would be the same as the containing shape but you have changed those (the code is not reading the caption-that just happens to be the same as the shape name). why do you need to access the name instead of the button itself?

  14. #14
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    Hello Joseph,

    I need to get the CommandButtonX, because I need to assign
    each button to a macro.

    And when you want to assign a macro when you do click over this kind of buttons you need to
    put the below code within sheet module where the button is located.

    For first command button should be:
    Please Login or Register  to view this content.
    Because of this, I want to know the name of each button to generate the code for each button
    automatically an put it inside sheet module.
    Regards

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

    Re: How to obtain Object name

    why do you need to add the code at runtime?

  16. #16
    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 obtain Object name

    Why don't you just double click the buttons?

    That'll generate the code you want.

  17. #17
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    I only want to generate de "Private Subs" code for each button, because all buttons need to execute the same
    macro.

    So each "Private Sub CommandButtonX" should go within sheet module, and since there are many buttons I don't want
    to do it manually.

    I hope make sense.

    Thanks in advance.

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

    Re: How to obtain Object name

    really? there aren't that many-much simpler to copy and paste then edit the number

  19. #19
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    I know that could be easier do it manually even that I could have more than 100 buttons, but the issue is
    that is needed to delete by macro the buttons, and depending the requeriments, create a number of new buttons,
    and this task could be very often.

    So, I would like to get automatically the association of those new buttons with the macro desired and put that
    code within sheet module.

    I hope make sense why I ask to do it by VBA code.

    Thanks in advance.

  20. #20
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: How to obtain Object name

    See the attachment
    Attached Files Attached Files



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

    Re: How to obtain Object name

    if your code creates the buttons the button name will be the same as the oleobject name so the original code I posted will work

  22. #22
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    Hello snb,

    Exactly! that code gives the name of the buttons!

    I know, that is not part of the original question, but if it is possible, how can be generated the "Private Sub CommandButtonX" for each button
    and take it inside Sheet module?

    Thanks for all help.

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

    Re: How to obtain Object name

    snb's code uses the same method I already posted-run it on your workbook and see what results you obtain

  24. #24
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: How to obtain Object name

    Why not use forms controls buttons? You can loop through the sheets buttons collection and assign a macro as a string. Seems much easier

  25. #25
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    Quote Originally Posted by JosephP View Post
    snb's code uses the same method I already posted-run it on your workbook and see what results you obtain
    Hello again Joseph,

    I'm not sure why, but I posted before, only prints the button name only for the first button, for the others prints the caption as below only prints the buttonīs names for all buttons in Inmediate window, but not in worksheet.

    This happens in the workbook I uploaded, but run it the same code in the book uploded by snb, works.

    In worsheet prints this:
    Please Login or Register  to view this content.
    Please try it:
    The code is:
    Please Login or Register  to view this content.
    Quote Originally Posted by Kyle123 View Post
    Why not use forms controls buttons? You can loop through the sheets buttons collection and assign a macro as a string. Seems much easier
    Hello Kyle,

    I'm using Activex buttons because are needed to use different colors and Form buttons only allowed me to use gray .


    If it is possible, only your help to generate the code for each button that will go inside sheet module and put it inside sheet module.

    Regards
    Last edited by cgkmal; 02-27-2013 at 07:14 PM.

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

    Re: How to obtain Object name

    I already told you why. if you run my code on snb's workbook it produces the same results as his and if you run his code on your workbook it produces the same results as mine because the container shapes have been renamed (the names are the same as the captions-the code does not read the captions)

  27. #27
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    I understand better that part JosephP. Thank you!

    Only If it is possible, your help to generate the code for each button that will go inside sheet module and how to
    put it inside sheet module.

    You have the idea of my goal now, and begin a new thread I'm not sure if it is good idea.

    Well, many thanks for all help so far.

  28. #28
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: How to obtain Object name

    To Illustrate JosepfP's point see attached
    Attached Files Attached Files
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  29. #29
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    Thank you jaslake for clarificate even more with that example.

    The last issue if somebody can help me.

    Only If it is possible, your help to generate the code for each button that will go inside sheet module and how to
    put it inside sheet module.

    Thanks in advance.

  30. #30
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: How to obtain Object name

    application.caller doesn't work for activex controls
    Yes, you're absolutely right. I ran my code on a form control command button

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

    Re: How to obtain Object name

    I understand your goal but I think it's a really bad idea because if you're adding and removing buttons at runtime you'll have to clean up existing event code as you go. perhaps you could explain the overall purpose and why you aren't using a ribbon menu system for example?

  32. #32
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: How to obtain Object name

    I'm afraid your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

  33. #33
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: How to obtain Object name

    Hello Kyle,

    As I explained you, I opened a question, somebody was helping me, in the solution he posted was missing a way
    to know the name of the buttons. That was the question I did here. And I understand the point, I try to help
    others too, not only ask.

    Anyway, thank you.

+ 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