ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Event Code for Controls created at runtime... (https://www.excelbanter.com/excel-programming/378841-re-event-code-controls-created-runtime.html)

Bob Phillips

Event Code for Controls created at runtime...
 
Here is some code that creates a checkbos, and adds an event into the
worksheet code module.


'-----------------------------------------------------------------
Sub CreateCheckBox()
'-----------------------------------------------------------------
Dim oWs As Worksheet
Dim oOLE As OLEObject

Set oWs = ActiveSheet

Set oOLE = ActiveSheet.OLEObjects.Add(ClassType:="Forms.Check Box.1", _
Left:=200, Top:=100, Width:=80, Height:=32)

'To set with a cell
'With Range("H2")
' Set oOLE =
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Check Box.1", _
' Left:=.Left, Top:=.Top, Width:=.Width,
Height:=.Height)
'End With

With oOLE
.Object.Caption = "Run myMacro"
.Name = "myMacro"
End With

With ThisWorkbook.VBProject.VBComponents(oWs.CodeName). CodeModule
.InsertLines .CreateEventProc("Click", oOLE.Name) + 1, _
vbTab & "If Range(""A1"").Value 0 Then " & vbCrLf & _
vbTab & vbTab & "Msgbox ""Hi""" & vbCrLf & _
vbTab & "End If"

End With

End Sub

--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)
"Neily" wrote in message
...
Hi,

Is there a way of creating event code for a control that is created at
runtime?

For example, I have a form which based on a user created set of questions,
builds the form with a check box and combobox for each questions. I can't
predict how many questions the user will create.

I would like to be able to have a change event for the combo box that
switches the adjacent checkbox.

When the code creates the checkboxes and comboboxes, I have standard names
for them and just increment the number at the end of each. ie
chkMyCheck1,
chkMyCheck2, etc. I thought I might be able to write the change event
based
on what I know the name will be but just write more change events than I
would possibly need.

Basically, is there a dynamic in code way of doing this?

Ta.

Neily





All times are GMT +1. The time now is 12:46 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com