Inserting an event procedure into sheet code

R

roybrew

I have the following code that adds a combo box to the ActiveCell in
the ActiveSheet and creates the event procedure for it in the
ActiveSheet as well. It all works fine, except that after it
completes, you are dropped down into the VBE for no apparent reason. No
errors have occcurred. Anyone seen this before?


The code is below:

Sub buildcombo()
Dim combo As OLEObject
Dim LineNum As Long
Dim CodeMod As Object

' Create the combo box on the ActiveCell on the ActiveSheet
Set combo =
ActiveSheet.OLEObjects.Add(classtype:="Forms.ComboBox.1", Link:=False,
DisplayAsIcon:=False, _
Left:=ActiveCell.Left, Top:=ActiveCell.Top,
Width:=ActiveCell.Width + 5, _
Height:=ActiveCell.Height + 5)

' now name the combobox and insert the handler for it into the
sheet code module
On Error GoTo err_trap
combo.Name = "ComboBox1"
Set CodeMod =
ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
With CodeMod
LineNum = .CreateEventProc("Change", combo.Name) + 1
.InsertLines LineNum, vbTab & "If (ComboBox1.ListIndex <> -1)
Then"
.InsertLines LineNum + 1, vbTab & vbTab & "Cells(10, 4).Select"
.InsertLines LineNum + 2, vbTab & vbTab & "ActiveCell.Value =
ComboBox1.List(ComboBox1.ListIndex)"
.InsertLines LineNum + 3, vbTab & "End If"
End With
Exit Sub
err_trap:
MsgBox "Got error = " & Err.Description
End Sub


Please forgive the word wrapping. Thanks in advance for any help.

Roy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top