+ Reply to Thread
Results 1 to 30 of 30

Dynamically catch any click event

  1. #1
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Dynamically catch any click event

    I am working on a project where I have a lot of labels. I want to be able to call a sub whenever any of these labels are clicked. Currently I am having to create a new code for each label, like this:
    Please Login or Register  to view this content.
    I would rather be able to create one code that can catch any label being clicked (calling the subroutine).

    Thank you for any help or suggestions on the matter. Feel free to ask further questions for clarification, curiosity, or any other reason.

  2. #2
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Dynamically catch any click event

    Not sure if this will help you...
    https://support.microsoft.com/en-us/kb/213566

  3. #3
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    Hello cyiangou,

    Thanks for the reference to this information. This will be my first time diving into class modules and event handlers. I followed those steps successfully and am still left wondering how to capture clicking a label as an event. (Also, I am wondering if there are other events I can capture somehow if the class object is something different).

    Thanks again,

    SocratesJC

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

    Re: Dynamically catch any click event

    What exactly did you try?
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    Hello Norie

    I have just began to toy around with it... I don't know how to use class modules. I followed these instructions successfully, but it catches the event of resizing a window (not the event of clicking a label).

    In a Class Module:
    Please Login or Register  to view this content.

    In a regular module:

    Please Login or Register  to view this content.

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

    Re: Dynamically catch any click event

    Have a look at the attached file.

    It's nothing too fancy, but hopefully will point you in the right direction.

    UFDynamicLabelClassEx.xls

  7. #7
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Dynamically catch any click event

    Hi SocratesJC. I'd love to help you (this is up my alley), but I have other work pressures on me right now. I'm sure this community will sort you out.

  8. #8
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    Thank you cyiangou, I understand work pressures.

    Norie,

    This is looking very promising. At first glance, it appears to be what I need except that I need something that catches 'sheet label clicks' and not 'userform label clicks'.

    I will study this and see what I can make of it. Thank you!

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

    Re: Dynamically catch any click event

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  10. #10
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    This is a very old version (because of the smaller size than the current). It gives you the idea. I am trying to have an infinite number of labels on the sheet that I can click and call a subroutine.
    Attached Files Attached Files

  11. #11
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Dynamically catch any click event

    I think you'll need to use ActiveX worksheet objects to do this. Have a look at this thread:

    http://www.ozgrid.com/forum/archive/.../t-174386.html

  12. #12
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    Hi cyiangou, I am putting great effort into understanding (or at least practicing) the code in that link. Most of it is pretty new to me.

    I should make a note that, I do not need to dynamically create images, or labels in my case (unless this is necessary in linking them to a class module). I only need to catch the click event on labels so that I do not have to keep creating new code for each label.

    Thank you for your help! I will certainly keep studying the subject.

  13. #13
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Dynamically catch any click event

    If it's only a label why not use shapes instead and assign the same macro to all of them?
    Cheers
    Andy
    www.andypope.info

  14. #14
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    Andy Pope, I am not certain that I am understanding you entirely. Isn't that sort of what i was doing before? Is there a way to assign a single macro to all of my labels without having to rewrite it every time? That is essentially my original question.

    cyiangou, I am beginning to understand the code you referred me to in that link a tiny bit. I successfully executed it in a blank project. My problem now is that I can only get the code to work in a single image... How do I use it for additional images?

  15. #15
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Dynamically catch any click event

    Your original post would suggest you had activex labels, which each has it's own click event. Within the events you are calling the same routine.

    If you want to keep activex labels then you need to use the classes approach.

    If you use shapes instead you can select all the shapes and assign the same macro.

  16. #16
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    I believe that I finally solved it. Here is what I did:

    1st, I add this code to a class module:

    Please Login or Register  to view this content.
    2nd, I add this code to a normal module:

    Please Login or Register  to view this content.
    3rd, I manually created 3 images on a worksheet.

    4th, I finally ran the sub contained in the macro (written above). This did the trick, I could now click any of the labels and run the same sub specified in the class module.

    A huge thank you to cyiangou!

  17. #17
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Dynamically catch any click event

    Socrates, that's a good point Andy makes. It's still manual, but the idea that you can multi-select many controls and 'Assign macro' once, should make things much easier.

  18. #18
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Dynamically catch any click event

    A huge thank you to cyiangou!
    My pleasure. Well done! Aren't you pleased you did it yourself?

  19. #19
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Dynamically catch any click event

    If you are adding the shapes via code you can set the OnAction property

  20. #20
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    cyiangou, I must say that it is true: I am quite pleased with myself. I am also deeply grateful of the help I've received here.

    Andy, I read somewhere recently about onaction property. I vaguely remember it being a property only available to certain types of objects. I'll look into it for future endeavors, but I have all I need now for this one.

    Thanks again everyone!

  21. #21
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    I do not believe anyone checks these threads after they have been solved, but I have a new problem. (I may have to start a new thread).

    The class module works great after I run the regular module code. The problem is, every time I run almost any other code, it suddenly stops my class module. I need to go and manually run the module again to get it going once again. I tried placing this code at the end of other codes and it doesn't seem to do a thing.

    Please Login or Register  to view this content.
    If I place that call code on a button, and press it after all codes are finished executing, then it works again, but I don't want to have to do this manually.

  22. #22
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Dynamically catch any click event

    It's fairly normal to lose the state of your module variables in Excel, especially when debugging.

    The normal fix is to have an IF check to see if things have reset, and if they have, call the reinitialisation code.

    But in this case, it's a catch-22, the code that would do this check is the very code that isn't being fired. Hmmm...

  23. #23
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    The odd thing is, even when I had a 'call' code,to reinitialize my code, added to the very end of all codes, the problem still remained. I used msgbox to determine that the code was in fact being 'reinitialized', but it ultimately still remained off when all was said and done.

    I narrowed the cause of the problem down to this code that I was running:

    Please Login or Register  to view this content.
    The problem seems to arise any time I try to add any objects to my sheet (or cut and paste objects also).

    I decided to go around the whole problem for now and simply have all objects pre-made so that I no longer need them to be created while code is running. Now I simply have all the objects visible property set to false to start. Then I use my code to make them visible (rather than make them exist).

    It isn't the best solution, but it should work for now (hopefully).

  24. #24
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Dynamically catch any click event

    It's only a problem you'll have when you're debugging. Once deployed and in 'run-mode', it'll go away.

  25. #25
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    It's only a problem you'll have when you're debugging. Once deployed and in 'run-mode', it'll go away.
    I don't know if you have taken a look at my project (the only posted one is a very old version), but it is a game made in excel. The way I have the game setup, there is no 'deployed and in run-mode'. Unfortunately, code in my game generally runs for a short while and then stops until further input (clicks etc.).

    It is only since I started with class modules does anything constantly run all the time. I have occasional Userforms that run for long periods of time (some of them with 0 modal).

    I have other games that run entirely in Userforms (these probably run a little more in 'full-run-mode'). Most people encourage me to stick to this route strictly: I've ignored them.

    Let me know if this makes any sense to you. I would love to email you a copy of the latest if you are interested.

  26. #26
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Dynamically catch any click event

    When I say 'run-mode', I really mean 'not constantly debugging code'. I actually think the problem has to do with variable scope, or session scope. Eg. Something resets the module variable state. You're not using the 'End' statement anywhere are you?

  27. #27
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Dynamically catch any click event


  28. #28
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    Quote Originally Posted by cyiangou View Post
    You're not using the 'End' statement anywhere are you?
    I use the 'End Sub' Statement all the time if that is what you are referring to. Like I said, most of my code simply finishes: it wasn't until recently, with class modules, that I began to have constant-running codes. If you took at the project, it might be more clear, but hopefully you have a good idea of what I mean. It is possible that I simply don't follow what you are saying.

    Thanks for your persistence and patience on this one. You've been a big help.

  29. #29
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Dynamically catch any click event

    I would love to email you a copy of the latest if you are interested.
    I am interested. I'd rather you posted it here, but say the word and I'll pm you my email address.

  30. #30
    Registered User
    Join Date
    09-04-2014
    Location
    WA, USA
    MS-Off Ver
    2007
    Posts
    69

    Re: Dynamically catch any click event

    I'm having the same problem as before in yet another project/Game I am working on. Here is the game:
    MiniMazeStripped23.xlsm

    The problem is that the class codes for clicking the label (Unit) always stop working after I open and close the userform.
    I even tried automatically (when the Userform closed) running a code to start the class codes back up (this used to work in other similar scenarios), but it is not helping here.

    Any help would be great!

+ 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. Addin and ActiveWorkbook catch the same event
    By Jibse in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-31-2015, 11:33 AM
  2. Click event of dynamically created label
    By ajaykgarg in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-03-2010, 10:09 AM
  3. Dynamically add pop up combo box with a click event
    By dovee in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2008, 12:27 PM
  4. [SOLVED] catch event insert a row or delete a row
    By Nader in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2006, 01:45 AM
  5. Catch column width event.
    By Artem Omelianchuk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-03-2005, 01:05 PM
  6. [SOLVED] Catch insert line event
    By Franck in forum Excel General
    Replies: 2
    Last Post: 04-04-2005, 09:06 AM
  7. How do I 'catch' a 'delete cells' event
    By Eric in forum Excel General
    Replies: 2
    Last Post: 03-09-2005, 04:06 PM

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