Hello everyone!
I am dynamically creating a bunch of ActiveX/OLEObject comboboxes. Each combobox needs to have a dynamically created Click event handler. I am able to do this, no prob.
The problem is that, occasionally during a session I want to delete all of the comboboxes and their associated event handlers and recreate them. The addCombos() method below first deletes existing comboboxes, then tries to delete click event handlers before creating new ones.
I've gotten very close to solving this but the spreadsheet keeps crashing part way through deleting the dynamically created click event handlers. Here's the code, I've got it loaded into "Sheet1":
The first time I run addCombos() everything works as planned. Comboboxes are created on Sheet1 and event handler subs are inserted at the top of the module.
If I run addCombos() again, it starts will successfully delete the first event handler, TestComboBox_5_Click() but then Excel crashes.
I'm running Excel 2016/64-bit. I have the following VBAProject references set in Tools > References:
Thank you very much for any help in advance. I am at my wit's end on this.
I am dynamically creating a bunch of ActiveX/OLEObject comboboxes. Each combobox needs to have a dynamically created Click event handler. I am able to do this, no prob.
The problem is that, occasionally during a session I want to delete all of the comboboxes and their associated event handlers and recreate them. The addCombos() method below first deletes existing comboboxes, then tries to delete click event handlers before creating new ones.
I've gotten very close to solving this but the spreadsheet keeps crashing part way through deleting the dynamically created click event handlers. Here's the code, I've got it loaded into "Sheet1":
Code:
Public Sub addCombos()
Dim thisRow As Integer
Dim thisCol As Integer
Dim thisLeft As Integer
Dim thisTop As Integer
Dim i As Integer
Dim obj As OLEObject
Dim thisName As String
Dim VBProj As Object
Dim vbCodeMod As Object
Dim count As Integer
Dim StartLine As Long
Dim NumLines As Long
Dim LineNum As Long
Dim ProcName As String
Dim ProcKind As VBIDE.vbext_ProcKind
Application.ScreenUpdating = False
Sheets("Sheet1").Activate
Set VBProj = ActiveWorkbook.VBProject
Set vbCodeMod = VBProj.VBComponents(ActiveSheet.CodeName).CodeModule
thisRow = 1
thisCol = 1
thisLeft = 0
thisTop = 0
' nuke any existing comboboxes
For Each obj In ActiveSheet.OLEObjects
obj.Delete
Next obj
' clear existing dynamically created code procedures
With vbCodeMod
LineNum = .CountOfDeclarationLines + 1
Do Until LineNum >= .CountOfLines
ProcName = .ProcOfLine(LineNum, ProcKind)
LineNum = .ProcStartLine(ProcName, ProcKind) + _
.ProcCountLines(ProcName, ProcKind) + 1
If Left(ProcName, 13) = "TestComboBox_" And Right(ProcName, 6) = "_Click" Then
StartLine = .ProcStartLine(ProcName, ProcKind)
NumLines = .ProcCountLines(ProcName, ProcKind)
.DeleteLines StartLine:=StartLine, count:=NumLines
End If
Loop
End With
' add a few new combo boxes
For i = 0 To 5
thisName = "TestComboBox_" + CStr(i)
Cells(thisRow, thisCol).Select
With Selection
thisLeft = .Left
thisTop = .Top
.RowHeight = 20
End With
Set obj = OLEObjects.Add(ClassType:="Forms.ComboBox.1", DisplayAsIcon:=False, Left:=thisLeft, Top:=thisTop, Width:=100, Height:=17)
With obj
.name = thisName
' now add items to each combobox
With obj.Object
.AddItem "Apple"
.AddItem "Orange"
.AddItem "Blueberry"
.Font.Size = 10
End With
End With
' dynamically add the various trigger events for this control,
' see: http://stackoverflow.com/questions/9476481/detecting-event-on-comboboxes-added-at-runtime-on-excel
vbCodeMod.AddFromString AddEvent(obj.name)
thisRow = thisRow + 1
Next i
End Sub
Private Function AddEvent(strIn As String) As String
AddEvent = "Public Sub " & strIn & "_Click()" & Chr(10) & _
"dim newVal As String" & Chr(10) & _
"newVal = ActiveSheet.OLEObjects(""" + strIn + """).Object.value" & Chr(10) & _
"MsgBox newVal" & Chr(10) & _
"End Sub"
End Function
The first time I run addCombos() everything works as planned. Comboboxes are created on Sheet1 and event handler subs are inserted at the top of the module.
If I run addCombos() again, it starts will successfully delete the first event handler, TestComboBox_5_Click() but then Excel crashes.
I'm running Excel 2016/64-bit. I have the following VBAProject references set in Tools > References:
- Visual Basic for Applications
- Microsoft Excel 16.0 Object Library
- OLE Automation
- Microsoft Office 16.0 Object Library
- Microsoft Forms 2.0 Object library
- Microsoft Visual Basic for Applications Extensibility 5.3
Thank you very much for any help in advance. I am at my wit's end on this.