removing controls from frame

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
529
I have a frame that I'm able to create dynamic buttons into it with VBA code.
At times, I want to clear the slate (the frame to be exact) but have had some issues. I originally used:

userform1.frame1.clear

Which seemed to work fine until I moved my project to another computer.
I then changed it to

Code:
For Each ctl In Userform1.frame1.Controls
    ctl.Visible = False
Next ctl

Which works but I feel is not the correct way of handling the removal of controls.

How do I remove all controls from frame1.
Here is the code I used for creating the buttons.

Code:
For Each cell In spTY.Range("ak2:ak" & iRow).Cells
        If cell.Value = "" Then GoTo nextitem
            Set ctl = UserForm1.Frame1.Controls.Add("Forms.CommandButton.1")
            ctl.Caption = cell.Value
            ctl.Font.Size = 12  'SET FONT SIZE TO 14
            ctl.BackStyle = 1
            ctl.BackColor = &HFFC0C0
            ctl.TabStop = False

    
            If zx < 2 Then
                ctl.Left = zx * 125 'adjust the width if you adjust this left. example: width 120 then left is 130
                ctl.Top = zq * 26 'adjust the height if you adjust this top. example: height 30 then top is
            Else:
                zx = "0"
                zq = zq + 1
                ctl.Left = zx * 125
                ctl.Top = zq * 26
            End If
            ctl.Font.Name = "Arial"
            ctl.Font.Bold = True
            ctl.Height = 24 '25
            ctl.Width = 120 '110
            ctl.WordWrap = True
            
            ReDim Preserve CmdButtons1(1 To i + 1)
            Set CmdButtons1(i + 1).CmdGroup1 = ctl
            i = i + 1
            z = z + 130
            zx = zx + 1
nextitem:

    Next cell
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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