How to delete dynamically created Combobox (ActiveX)?

AlexExcel2021

New Member
Joined
Feb 23, 2023
Messages
25
Office Version
  1. 2021
Platform
  1. Windows
I have created Combobox and try to delete it

VBA Code:
     Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     Dim Combo As ComboBox
     On Error GoTo SelectAll
         If Selection.Count = 1 Then
             If Not Intersect(Target, Columns("A")) Is Nothing Then
                SaveSetting "ExcelHelper", "Data", "ManRow", Target.Row - 1
                CreateCombo 0, Target.RowHeight * (Target.Row - 1), 80, Target.RowHeight
                With ManCombo
                 .Activate = True
                 .Visible = True
                 .Activate
                End With
             Else
                 DeleteAllCombo
             End If
         End If
         Exit Sub
     SelectAll:
     End Sub

     Option Explicit
     Public Sub CreateCombo(Left, Top, Width, Heigth)
       Dim Combo As ComboBox
       Dim TheComboName As String
       TheComboName = "ManCombo"
       On Error Resume Next
         ActiveSheet.OLEObjects(TheComboName).Delete
       On Error GoTo 0
       Set Combo = ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
         Link:=False, DisplayAsIcon:=False, Left:=Left, Top:=Top, Width:=Width, Height:=Heigth).Object
       Combo.Name = "ManCombo"
     End Sub

     Public Sub DeleteAllCombo()
         Dim wks As Worksheet
         Dim obj As OLEObject
         Set wks = ActiveSheet
         For Each obj In wks.OLEObjects
             If obj.progID = "Forms.CheckBox.1" Then obj.Delete
         Next
     End Sub


But Combobox not deleted in this way. Hmmm, why?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I have created Combobox and try to delete it

VBA Code:
     Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     Dim Combo As ComboBox
     On Error GoTo SelectAll
         If Selection.Count = 1 Then
             If Not Intersect(Target, Columns("A")) Is Nothing Then
                SaveSetting "ExcelHelper", "Data", "ManRow", Target.Row - 1
                CreateCombo 0, Target.RowHeight * (Target.Row - 1), 80, Target.RowHeight
                With ManCombo
                 .Activate = True
                 .Visible = True
                 .Activate
                End With
             Else
                 DeleteAllCombo
             End If
         End If
         Exit Sub
     SelectAll:
     End Sub

     Option Explicit
     Public Sub CreateCombo(Left, Top, Width, Heigth)
       Dim Combo As ComboBox
       Dim TheComboName As String
       TheComboName = "ManCombo"
       On Error Resume Next
         ActiveSheet.OLEObjects(TheComboName).Delete
       On Error GoTo 0
       Set Combo = ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
         Link:=False, DisplayAsIcon:=False, Left:=Left, Top:=Top, Width:=Width, Height:=Heigth).Object
       Combo.Name = "ManCombo"
     End Sub

     Public Sub DeleteAllCombo()
         Dim wks As Worksheet
         Dim obj As OLEObject
         Set wks = ActiveSheet
         For Each obj In wks.OLEObjects
             If obj.progID = "Forms.CheckBox.1" Then obj.Delete
         Next
     End Sub


But Combobox not deleted in this way. Hmmm, why?
Ups...wrong type, Checkbox instead Combobox, sorry
 
Upvote 0
I modified your code slightly. Try this.

VBA Code:
Public Sub DeleteAllCombo()
         Dim wks As Worksheet
         Dim obj As OLEObject
         Set wks = ActiveSheet
         For Each obj In wks.OLEObjects
             If obj.Name = "CheckBox1" Then obj.Delete
         Next
     End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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