+ Reply to Thread
Results 1 to 10 of 10

Cannot delete a control from a userform using VBA

  1. #1
    Registered User
    Join Date
    03-18-2014
    Location
    Florida, USA
    MS-Off Ver
    Office 365
    Posts
    8

    Cannot delete a control from a userform using VBA

    Hello:

    I have made a good-faith attempt to search the forum for the answer but am unable to find a workable solution so please bear with me.

    Here is the situation:

    - I have a userform with a number of ActiveX controls: command buttons, labels, etc.
    - While trying to duplicate some of them, I end up with some with the generic label CommandButton1, Label3, etc.
    - I wish to delete these extraneous controls using VBA but run into a problem

    My code looks like this:

    Please Login or Register  to view this content.

    When I use the variable badControl as shown above, I get the error: "Run-time error '438': Object doesn't support this property of method"

    When I use the index number (as in: MyUserForm.Controls.Remove (controlNum - 1) suggested by one forum post), I get the error: "Run-time error '444': Could not delete the controls. This method can't be used in this context"

    The answer is probably quite trivial but I can't figure out what I am missing.

    Thanks for your assistance.
    Last edited by AliGW; 08-02-2021 at 03:49 AM.

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

    Re: Cannot delete a control from a userform using VBA

    The help on Remove says,

    This method deletes any control that was added at run time. However, attempting to delete a control that was added at design time will result in an error.

    Rather than delete could you not just make them invisible?
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,887

    Re: Cannot delete a control from a userform using VBA

    Administrative Note:

    Welcome back to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. By way of a reminder, I've done it for you today.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Cannot delete a control from a userform using VBA

    If this is a one-off clean up operation, you should be using the Designer property of the relevant VBComponent - for example:

    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Registered User
    Join Date
    03-18-2014
    Location
    Florida, USA
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Cannot delete a control from a userform using VBA

    Many thanks, Ali. It has been a LONG time since I have been on the forum. I will comply in the future.

    Dereck

  6. #6
    Registered User
    Join Date
    03-18-2014
    Location
    Florida, USA
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Cannot delete a control from a userform using VBA

    Hello Andy:

    Thanks for that suggestion. I don't see them on the form, so they do not affect anything. It is just that when I list the controls they show up. I was just trying to keep things clean.

    Kind regards,

    Dereck

  7. #7
    Registered User
    Join Date
    03-18-2014
    Location
    Florida, USA
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Cannot delete a control from a userform using VBA

    Hello Rory:

    Thanks for that suggestion. I tried it but get a different error: "Run-time error: '1004': Method 'VBProject' of object '_Workbook' failed"

    Since I don't actually see these controls and they don't affect anything, I will let it go at that.

    Again, thanks for your assistance.

    Regards,

    Dereck

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Cannot delete a control from a userform using VBA

    You would need to enable access to the VBA project in the Trust Center in macro settings, or just delete them yourself.

  9. #9
    Registered User
    Join Date
    03-18-2014
    Location
    Florida, USA
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Cannot delete a control from a userform using VBA

    Quote Originally Posted by rorya View Post
    You would need to enable access to the VBA project in the Trust Center in macro settings, or just delete them yourself.
    Well, Rory, I followed your advice and, lo and behold, it works!!! I had assumed that I had access to the VBA project by default.

    Many thanks for your assistance. This has been bugging me for quite some time now.

    Best regards,

    Dereck

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Cannot delete a control from a userform using VBA

    Glad we could help, and thanks for the rep.

+ 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. [SOLVED] Adding delete code on userform to delete a row on sheet by selecting item on a listbox
    By darbar76528 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 11-07-2020, 12:50 PM
  2. Exit Userform control and revert to previous control value code - ESC key equiv
    By ddeluca in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2020, 08:18 PM
  3. [SOLVED] Excel 2007, Userform Textbox Date Format and Calendar Control Userform
    By riffology in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-03-2014, 06:18 PM
  4. [SOLVED] Can add listbox control to userform, but cannot delete!
    By perducci in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-21-2013, 02:03 PM
  5. UserForm Control
    By DCSwearingen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-24-2007, 10:48 AM
  6. Dim Userform Control
    By donbowyer in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-27-2005, 08:05 PM
  7. delete Userform control programmatically
    By ben in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-15-2005, 06:45 PM

Tags for this Thread

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