delete Combo Box (created through ActiveX)

krrishkrsna

Board Regular
Joined
Jan 31, 2009
Messages
84
i am placing below code in ThisWorkbook (Excel VBA). whenever i go to new sheet the ComboBox1 should get created if it does not exist and should be deleted if already exists and create new combobox1, but getting below error

Run Time Error -2147024809(80070057) The item with the specified name was not found

If Not ActiveSheet.Shapes("ComboBox1").Name = "ComboBox1" Then
ActiveSheet.OLEObjects.Add(classtype:="Forms.ComboBox.1", link:=False, displayasicon:=False, Left:=100, Top:=50, _
Width:=120, Height:=28.5).Select
Else
ActiveSheet.Shapes("ComboBox1").Delete
End If

Please HELP
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
If Not ActiveSheet.Shapes("ComboBox1").Name = "ComboBox1" Then
You can't reference an object if it doesn't exist, so it sends the error.
I show you 2 ways to do it, the first is to look for the combo, if it exists, delete it.

VBA Code:
Sub createcombobox()
  Dim sp As Shape
  
  For Each sp In ActiveSheet.Shapes
    If sp.Name = "ComboBox1" Then
      ActiveSheet.Shapes("ComboBox1").Delete
    End If
  Next
  ActiveSheet.OLEObjects.Add(classtype:="Forms.ComboBox.1", link:=False, displayasicon:=False, Left:=100, Top:=50, _
    Width:=120, Height:=28.5).Select
End Sub

Or just remove it, if it exists remove it, if it doesn't exist with the On Error instruction, it will go to the next instruction and don't send the error.

VBA Code:
Sub createcombobox_2()
  On Error Resume Next
  ActiveSheet.Shapes("ComboBox1").Delete
  On Error GoTo 0
  
  ActiveSheet.OLEObjects.Add(classtype:="Forms.ComboBox.1", link:=False, displayasicon:=False, Left:=100, Top:=50, _
    Width:=120, Height:=28.5).Select
End Sub
 
Upvote 0
You can't reference an object if it doesn't exist, so it sends the error.
I show you 2 ways to do it, the first is to look for the combo, if it exists, delete it.

VBA Code:
Sub createcombobox()
  Dim sp As Shape
 
  For Each sp In ActiveSheet.Shapes
    If sp.Name = "ComboBox1" Then
      ActiveSheet.Shapes("ComboBox1").Delete
    End If
  Next
  ActiveSheet.OLEObjects.Add(classtype:="Forms.ComboBox.1", link:=False, displayasicon:=False, Left:=100, Top:=50, _
    Width:=120, Height:=28.5).Select
End Sub

Or just remove it, if it exists remove it, if it doesn't exist with the On Error instruction, it will go to the next instruction and don't send the error.

VBA Code:
Sub createcombobox_2()
  On Error Resume Next
  ActiveSheet.Shapes("ComboBox1").Delete
  On Error GoTo 0
 
  ActiveSheet.OLEObjects.Add(classtype:="Forms.ComboBox.1", link:=False, displayasicon:=False, Left:=100, Top:=50, _
    Width:=120, Height:=28.5).Select
End Sub
procedure CreateComboBox works perfectly (procedure CreateComboBox_2, throws the error which i mentioned)

googled for code and tried all those but failed. finally u helped me. thank your very much🙏
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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