Label_Click executes sub twice

d365b

New Member
Joined
Dec 26, 2021
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I am using the solution suggested by Colin Legg in the thread below


but when I call a sub within the Label_Click sub

VBA Code:
Private Sub pLabel_Click()
    clsCalendar.ClickEventHandler (pLabel.Name)
End Sub

the clsCalendar.ClickEventHandler gets executed twice. Any idea why this is happening? Is there a way to avoid that or a suitable workaround?

Many thanks for all your suggestions! :)
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
The name of clsCalendar.ClickEventHandler suggests that it's an event handler.
Event handlers are automatically invoked by Excel when the event in question occurs.
If multiple handlers are implemented for the exact same type of event, they will be executed sequentially.

In your example, the Label_Click event handler apparently invokes another Label_Click handler, causing it to run twice.
 
Upvote 0
The name of clsCalendar.ClickEventHandler suggests that it's an event handler.
Event handlers are automatically invoked by Excel when the event in question occurs.
If multiple handlers are implemented for the exact same type of event, they will be executed sequentially.

In your example, the Label_Click event handler apparently invokes another Label_Click handler, causing it to run twice.
Hi GWteB,

thank you for replying to my thread! Well, the Label_Click only has to capture the name of the label that was clicked and pass it on to the ClickEventHandler which carries out different tasks according to the label that was clicked.

I am trying to build a MonthView from scratch and could not figure out how to have the Label_Click handler in the Calendar class. I had to have a separate class for the label. I had to use a flag (boolRunOnce) as a workaround. Unfortunately, I cannot upload the workbook here: it would have been much easier to explain myself.
 
Upvote 0
What does the code for clsCalendar.ClickEventHandler look like? I suspect your implementation has gone amiss somewhere but it may be hard to diagnose without all the code (you can always upload to a site like Onedrive or Dropbox and post a link here?). Also note that there is no need for the parentheses around pLabel.Name.
 
Upvote 0
What does the code for clsCalendar.ClickEventHandler look like? I suspect your implementation has gone amiss somewhere but it may be hard to diagnose without all the code (you can always upload to a site like Onedrive or Dropbox and post a link here?). Also note that there is no need for the parentheses around pLabel.Name.
Right, I could share it as you suggested RoryA.

Here's the link.
 
Upvote 0
I've downloaded your file, will reply shortly
 
Upvote 0
No doubt you will have noticed that the phenomenon you described only occurred when clicking on labels that act as navigation keys (forward, back, up and down). The reason for this was the following.

The LoadView method of the Calendar class turned out to be responsible for two tasks:
- initialization when opening the userform;
- updating the view in response of user input by practically rebuilding your entire userform.

Because the latter was triggered by a click event, the code of that click event handler was kept alive while a new event handler was implemented at the same time. Only after the very last code of the event handler had been executed the code got out of scope and the corresponding class was terminated. At the time of this termination, the newly supplied event handler for the event in question was already present so it was executed by Excel, making it look like the same code was being executed twice, but in fact it was a different instance of that class.

I made some minor changes of which the most eye-catching are marked with @@@@@. The two tasks are now in two separate procedures: a public Init method and a private LoadView method. The Init method is responsible for initializing the permanent Label controls, the LoadView for the temporary ones. So there are also two collections used for storage: one to keep the permanent instances of the cLabel class, and one for the temporary ones. You might consider to make the date labels permanent as well and only changing the caption of those labels in response to user input.

I'm aware that the changes I've made might not be very attractive on the surface, nevertheless they solve your issue.

Finally, a few notes. Disabling Excel events has no effect on the events of ActiveX controls on a userform. Excel's screen updating doesn't care about any dynamic changes to a userform and Excel's calculation event is only fired if something actually changes on a worksheet, and therefore has no influence on the performance of any change of a userform. So where you thought you would achieve a performance improvement, you actually got a slight performance penalty.


Example Workbook
 
Upvote 0
No doubt you will have noticed that the phenomenon you described only occurred when clicking on labels that act as navigation keys (forward, back, up and down). The reason for this was the following.

The LoadView method of the Calendar class turned out to be responsible for two tasks:
- initialization when opening the userform;
- updating the view in response of user input by practically rebuilding your entire userform.

Because the latter was triggered by a click event, the code of that click event handler was kept alive while a new event handler was implemented at the same time. Only after the very last code of the event handler had been executed the code got out of scope and the corresponding class was terminated. At the time of this termination, the newly supplied event handler for the event in question was already present so it was executed by Excel, making it look like the same code was being executed twice, but in fact it was a different instance of that class.

I made some minor changes of which the most eye-catching are marked with @@@@@. The two tasks are now in two separate procedures: a public Init method and a private LoadView method. The Init method is responsible for initializing the permanent Label controls, the LoadView for the temporary ones. So there are also two collections used for storage: one to keep the permanent instances of the cLabel class, and one for the temporary ones. You might consider to make the date labels permanent as well and only changing the caption of those labels in response to user input.

I'm aware that the changes I've made might not be very attractive on the surface, nevertheless they solve your issue.

Finally, a few notes. Disabling Excel events has no effect on the events of ActiveX controls on a userform. Excel's screen updating doesn't care about any dynamic changes to a userform and Excel's calculation event is only fired if something actually changes on a worksheet, and therefore has no influence on the performance of any change of a userform. So where you thought you would achieve a performance improvement, you actually got a slight performance penalty.


Example Workbook
Wow! It is way more fluent than it used to be ... that's right: it only happened when clicking the navigation key labels. I am going to try making the changes you suggested: keeping all the labels (which will be added via the Init method) and changing only the caption (via the LoadView method). Next, I'll try to make it pop-up when double-clicking on a cell ... lots to do ?

Many thanks!
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,153
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top