Changing Event Subs of Controls On Air

H

Haldun Alay

Hi,

I'm designing a userform with several controls (Textboxes, Comboboxes,
CommandButtons etc.). But I create some controls on air via code. I want to
assign some subs ON AIR to control's event procs like On Enter, On Change,
On Exit... Does anybody know how can I do that? Thanks in advance...




--
Regards

Haldun Alay

To e-mail me, please remove AT and DOT from my e-mail address.
 
H

Haldun Alay

Hi Ron,

Thanks for your reply.

I try the codes on the site that you adviced but the problem is still exist.

For example I have a textbox control : TextBox1 and it's event sub
TextBox1_Enter() already defined.
Also I have one sub called AssignThis() in userform's code. What I want to
do is, While running userform, in some conditions I want to change
TextBox1's OnEnter event's sub to AssignThis via versa. On that site it
shows how to add events whose are not exist.


--
Regards

Haldun Alay

To e-mail me, please remove AT and DOT from my e-mail address.
 
R

Ron de Bruin

Here is a example for delete and add a sub
Delete it first and add a new one

You can use this for events also

Sub AddProcedure()
Dim VBCodeMod As CodeModule
Dim LineNum As Long

Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("Sheet1").CodeModule
With VBCodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, _
"Sub MyNewProcedure()" & Chr(13) & _
" Msgbox ""Here is the new procedure"" " & Chr(13) & _
"End Sub"
End With
End Sub


Sub DeleteProcedure()
Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim HowManyLines As Long

Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("Sheet1").CodeModule
With VBCodeMod
StartLine = .ProcStartLine("MyNewProcedure", vbext_pk_Proc)
HowManyLines = .ProcCountLines("MyNewProcedure", vbext_pk_Proc)
.DeleteLines StartLine, HowManyLines
End With
End Sub
 

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