Programmatically add/delete a control in a worksheet

  • Hi,


    I have a worksheet with certain cells that, when I double-click on them, will show a combobox containing all items already in the column. I used a dynamic range for that. The code is as follows:


    And I have another code in the SelectChange event to stash away the combobox when not in use, like this:


    This works pretty well as long as I use this code in my computer. If I were to import this code into the same worksheet on another computer, I'd have to manually go into the Developer's tab, insert a combobox somewhere in the sheet, name it "cboCombo" and set its initial properties, or else the code won't work.


    So my questions are:
    1. How do I create this combobox, set its initial properties, and delete after use on-the-fly, using just the VBA code? It's an ActiveX control, not a form control.
    2. How do I check if this combobox already exists in the sheet, so I don't have to add it?


    The ideal solution would be that, I just import this code, and any worksheet with this layout would get this combobox function, without the need to manually add a new control. And if the control is already there, skip the whoe thing.

  • Re: Programmatically add/delete a control in a worksheet


    Excel already has a Pick from List feature that does this. Right click on a cell that has entries in a column & choose Pick from List

  • Re: Programmatically add/delete a control in a worksheet


    Well, I didn't know you could do that. Thanks.
    But that wasn't exactly the answer I was looking for.
    The combobox was just an example. Could be a spinner for fine-tuning or a simple userform, based on the selected cell. Create and remove controls on the fly, using just the code.

  • Re: Programmatically add/delete a control in a worksheet


    This example creates a Label which contains unique entries from the column and resizes to the width of the column. I've also added an example for a ListBox.

  • Re: Programmatically add/delete a control in a worksheet


    Quote from royUK;800220

    This example creates a Label which contains unique entries from the column and resizes to the width of the column. I've also added an example for a ListBox.


    Thanks! This is the kind of code I was looking for, "dynamic" stuff (just didn't know how to properly phrase it). I'll just need to figure out how it works and implement the technique in my own code.
    Very cool and useful stuff. Thank you! :)

  • Re: Programmatically add/delete a control in a worksheet


    Here's an example of a UserForm, click into A1

  • Re: Programmatically add/delete a control in a worksheet


    Wow, you are amazing...
    I'd been Googling for this kind of stuff for ages without success, and you hacked them up in just minutes. This is so valuable. Thank you so much!

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!