AlexExcel2021
New Member
- Joined
- Feb 23, 2023
- Messages
- 28
- Office Version
- 2021
- Platform
- Windows
I have created Combobox and try to delete it
But Combobox not deleted in this way. Hmmm, why?
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?