Controls cloning unexpectedly!

Vladislav

New Member
Joined
Aug 21, 2011
Messages
33
Hi everyone,

This has got to do with creating, altering and deleting controls at runtime.

I've got a label being created at runtime (i.e. controls.add("Forms.Label.1", "myLabel",True) and have it assigned to a class to handle its events. Another procedure that searches for this control using FOR EACH ctl IN me.controls, IF ctl.name = "myLabel" THEN do some formatting of ctl.

The problem is that it somehow clones the original label when doing the formatting. I only discovered this by changing the position of ctl and seeing two copies, the original plus the newly cloned and correctly formatted version.

The workaround is using zorder to bring the clone forward and backward as required. However, I'd rather work out how to stop the control from cloning in the first place.

PS: I'd be happy enough if I could delete the cloned label, but I'm not sure of the syntax.. I've tried DELETE ctl and ctl.delete already.

Any ideas? Thanks!
Vlad
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Aren't you deleting the label before creating it? I expect that the proliferation is a result of your code rather than any cloning.
 
Upvote 0
But your code may have created it more than once. Wouldn't it be simpler to have a hidden control and make it visible when needed?
 
Upvote 0
Is this a control on a worksheet or on a userform?
If a userform, are you using the .Designer object?
What is the code for creation and formatting?
I agree that creating controls at run-time is seldom the best way to do things.
 
Upvote 0
You're right, silly me, thanks! There are lots of controls (42), it is easier having them created at runtime.
 
Upvote 0
Vlad

When and where are you creating the control?

Also what formatting are you doing and how/when are you looping?

Can you post the code?
 
Upvote 0
FWIW, I have never had an issue with controls created at run time being duplicated, so I'd suspect Andrew is correct.
 
Upvote 0
You're absolutely correct. I forgot to delete them before adding each time. I'm now stuck on trying to work out how to delete controls added at runtime. If there isn't a way, I'll just have to do something with the names or some other workaround.
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,472
Members
452,915
Latest member
hannnahheileen

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