How to Delete Excel UserForm Controls with VBA
Question details
The user needs to programmatically remove duplicate or unneeded controls from an Excel UserForm using VBA code.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating the cleanup of an Excel UserForm by deleting duplicate controls such as frames, labels, and text boxes through a macro.
- Observed behavior
- Duplicate controls are present on the UserForm, and they must be deleted dynamically via the VBA project object model rather than being removed manually in the editor.
Before running any code that modifies the VBA project, ensure you have enabled programmatic access to the VBA object model in your security settings and created a backup of your workbook.
Delete UserForm Controls using VBA Extensibility
Access the UserForm's designer object through the VBA Extensibility library to programmatically remove controls by their name.
To manipulate UserForms and their controls programmatically, you must first enable programmatic access to the Visual Basic Project and add the necessary Extensibility reference in the VBA Editor. This allows your macro to interact with the form's designer interface.
Open Excel, go to File > Options > Trust Center > Trust Center Settings. Select 'Macro Settings', check the box for 'Trust access to the VBA project object model', and click OK.
Press ALT + F11 to open the VBA Editor. Go to Tools > References in the top menu, scroll down to check 'Microsoft Visual Basic for Applications Extensibility 5.3', and click OK.
Insert a new module (Insert > Module) and declare your variables. Use the code: Set vbComp = ThisWorkbook.VBProject.VBComponents("UserForm1") followed by Set vbUserForm = vbComp.Designer to access the form.
Add the removal command: vbUserForm.Controls.Remove "Frame2". Replace 'UserForm1' and 'Frame2' with the actual names of your form and the control you wish to delete. Run the macro to execute the deletion.

Manage Macros and VBA UserForms with WPS Spreadsheet
WPS Spreadsheet provides robust support for macros and VBA, allowing you to create, edit, and programmatically manage UserForms and controls just like in Excel.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your macro-enabled spreadsheet.
- 2. Access the VBA Editor: Navigate to the Developer tab on the ribbon and click on 'Visual Basic' or press ALT + F11.
- 3. Edit UserForms: Use the built-in VBA editor to add, remove, or modify UserForm controls using standard VBA code.
- 4. Run Your Macro: Execute your code to programmatically manage your UserForms and save your workbook seamlessly.

Frequently Asked Questions
Why do I get a 'Programmatic access to Visual Basic Project is not trusted' error?
This occurs when Excel's security settings block macros from modifying the VBA project. To fix it, go to File > Options > Trust Center > Trust Center Settings > Macro Settings, and enable 'Trust access to the VBA project object model'.
Can I delete UserForm controls while the UserForm is open?
No, you cannot modify the UserForm designer while the form is actively loaded or displayed. Ensure the UserForm is closed and modify it via the Designer object in your code.
How do I find the exact name of the control I want to delete?
Open the VBA Editor (ALT + F11), double-click your UserForm to view it, select the control you want to delete, and check its '(Name)' property in the Properties window (press F4 if it is not visible).
Is the VBA Extensibility reference mandatory for this task?
Yes, adding the 'Microsoft Visual Basic for Applications Extensibility' library is required because it provides the necessary objects (like VBProject and VBComponent) to manipulate the VBA code and forms programmatically.




