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:
Sub Cleanup_Userform()
Dim badControl As MSForms.Control
Dim controlNum As Integer
controlNum = 0
For Each badControl In MyUserForm.Controls
Debug.Print controlNum & " - " & badControl.Name
controlNum = controlNum + 1
If Left(badControl.Name, 7) = "Command" Or _
Left(badControl.Name, 5) = "Combo" Or _
Left(badControl.Name, 5) = "Frame" Or _
Left(badControl.Name, 5) = "Image" Or _
Left(badControl.Name, 5) = "Label" Or _
Left(badControl.Name, 7) = "TextBox" Then MyUserForm.Controls.Remove (badControl)
Next badControl
End Sub
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.
Bookmarks