Dim MyComboBoxEvents As clsComboBoxEvents
Dim myCollection As Collection
Function AddComboBoxes(aSheet As Worksheet, r As Integer, c As Integer) As OLEObject
Dim cb As OLEObject
Dim aCell As Range
Set aCell = Cells(r, c)
Set cb = aSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
Left:=aCell.Left + 1, Top:=aCell.Top + 1, _
Width:=aCell.Width, Height:=aCell.Height)
cb.Placement = 1 '-- move and resize with cell
'cb.Name = "ComboBoxR" & r & "C" & c
cb.LinkedCell = aCell.Address
cb.Object.SpecialEffect = 0
cb.Object.BorderStyle = 1
'cb.Object.ShowDropButtonWhen = 1
Set AddComboBoxes = cb
End Function
Function RemoveComboBoxes(aSheet As Worksheet, r As Integer, Optional c As Integer = 0)
Dim ObjLink As String
Dim obj As OLEObject
If c = 0 Then
ObjLink = "$*$" & r
Else
ObjLink = Cells(r, c).Address
End If
For Each obj In aSheet.OLEObjects
If obj.LinkedCell Like ObjLink Then
aSheet.OLEObjects(obj.Name).Delete
End If
Next
End Function
Function HideComboBoxes(aSheet As Worksheet, r As Integer, c As Integer)
Dim ObjLink As String
Dim obj As OLEObject
ObjLink = Cells(r, c).Address
For Each obj In aSheet.OLEObjects
If obj.LinkedCell Like ObjLink Then
aSheet.OLEObjects(obj.Name).Visible = False
End If
Next
End Function
Function CmbObjExists(aRange As Range) As String
Dim obj As OLEObject
For Each obj In ActiveSheet.OLEObjects
If obj.LinkedCell = aRange.Address Then
CmbObjExists = obj.Name
Exit Function
End If
Next
End Function
Function CmbObjLink(aName As String) As Range
Dim obj As OLEObject
Set obj = ActiveSheet.OLEObjects(aName)
Set CmbObjLink = Range(obj.LinkedCell)
End Function
Sub collCtls()
Dim oleObj As OLEObject
Set myCollection = New Collection
For Each oleObj In ActiveSheet.OLEObjects
Set MyComboBoxEvents = New clsComboBoxEvents
Set MyComboBoxEvents.Ctl = oleObj.Object
myCollection.Add MyComboBoxEvents, oleObj.Name
Next
End Sub