ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Shorter way to create multiple identical Event Procedures? (https://www.excelbanter.com/excel-programming/409862-shorter-way-create-multiple-identical-event-procedures.html)

Sam Kuo[_3_]

Shorter way to create multiple identical Event Procedures?
 
Hi all,

I have 10 textboxes (namely txtPost1, txtPost2, ..., txtPost10) which I want
to assign the event procedure below to. I just wonder if there might be a
short way to do than copy and paste the code 10 times?

Private Sub txtPost1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update work duration (by calling macro UpdateDuration)
UpdateDuration ("Post")
End Sub

Jon Peltier

Shorter way to create multiple identical Event Procedures?
 
John Walkenbach shows here how to do this for buttons. Same idea,

http://www.j-walk.com/ss/excel/tips/tip44.htm

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Sam Kuo" wrote in message
...
Hi all,

I have 10 textboxes (namely txtPost1, txtPost2, ..., txtPost10) which I
want
to assign the event procedure below to. I just wonder if there might be a
short way to do than copy and paste the code 10 times?

Private Sub txtPost1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update work duration (by calling macro UpdateDuration)
UpdateDuration ("Post")
End Sub




Sam Kuo[_3_]

Shorter way to create multiple identical Event Procedures?
 
Thanks Jon. Below is my attempt, but I don't know what the error means and
how to tackle it. Do you mind have a look and give it a twig please? Many
thanks!

' Below is saved in Forms/frmUSLE:
'Compile error: Object does not source automation events --
Public WithEvents txtEarthwksGroup As TextBox

Private Sub txtEarthwksGroup_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update work duration (by calling macro UpdateDuration)
UpdateDuration ("Earthwks")
End Sub

' Below is saved in Class Modules/Class1:
Option Explicit
Dim TextBoxes() As New Class1

Private Sub RuntxtEarthwksGroup()
Dim ctrl As Controls
Dim CatchmentCount As Integer

CatchmentCount = 1
For Each ctl In frmUSLE.Controls
If TypeName(ctrl) = "TextBox" Then
If ctrl.Name = "txtEarthwks" & CatchmentCount Then
CatchmentCount = CatchmentCount + 1
ReDim Preserve TextBoxes(1 To CatchmentCount)
Set TextBoxes(CatchmentCount).txtEarthwksGroup = ctrl
End If
End If
Next ctrl
End Sub



"Jon Peltier" wrote:

John Walkenbach shows here how to do this for buttons. Same idea,

http://www.j-walk.com/ss/excel/tips/tip44.htm

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Sam Kuo" wrote in message
...
Hi all,

I have 10 textboxes (namely txtPost1, txtPost2, ..., txtPost10) which I
want
to assign the event procedure below to. I just wonder if there might be a
short way to do than copy and paste the code 10 times?

Private Sub txtPost1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update work duration (by calling macro UpdateDuration)
UpdateDuration ("Post")
End Sub





Sam Kuo[_3_]

Shorter way to create multiple identical Event Procedures?
 
Sorry, some corrections, but error still occurs...

' Below is saved in Class1
'Compile error: Object does not source automation events --
Public WithEvents txtEarthwksGroup As TextBox

Private Sub txtEarthwksGroup_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update work duration (by calling sub UpdateDuration in Module1)
UpdateDuration ("Earthwks")
End Sub


' Below is saved in Module1
Option Explicit

Dim TextBoxes() As New Class1
Sub RuntxtEarthwksGroup()
Dim ctrl As Controls
Dim CatchmentCount As Integer

CatchmentCount = 0
For Each ctl In frmUSLE.Controls
If TypeName(ctrl) = "TextBox" Then
If ctrl.Name = "txtEarthwks" & CatchmentCount Then
CatchmentCount = CatchmentCount + 1
ReDim Preserve TextBoxes(1 To CatchmentCount)
Set TextBoxes(CatchmentCount).txtEarthwksGroup = ctrl
End If
End If
Next ctrl
End Sub


Sub USLE()
RuntxtEarthwksGroup
' Show the userform frmUSLE
frmUSLE.Show
End Sub

"Jon Peltier" wrote:

John Walkenbach shows here how to do this for buttons. Same idea,

http://www.j-walk.com/ss/excel/tips/tip44.htm

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Sam Kuo" wrote in message
...
Hi all,

I have 10 textboxes (namely txtPost1, txtPost2, ..., txtPost10) which I
want
to assign the event procedure below to. I just wonder if there might be a
short way to do than copy and paste the code 10 times?

Private Sub txtPost1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update work duration (by calling macro UpdateDuration)
UpdateDuration ("Post")
End Sub





Jon Peltier

Shorter way to create multiple identical Event Procedures?
 
The VBA compiler thinks you mean a worksheet textbox. Change to this:

Public WithEvents txtEarthwksGroup As MSForms.TextBox

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Sam Kuo" wrote in message
...
Sorry, some corrections, but error still occurs...

' Below is saved in Class1
'Compile error: Object does not source automation events --
Public WithEvents txtEarthwksGroup As TextBox

Private Sub txtEarthwksGroup_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update work duration (by calling sub UpdateDuration in Module1)
UpdateDuration ("Earthwks")
End Sub


' Below is saved in Module1
Option Explicit

Dim TextBoxes() As New Class1
Sub RuntxtEarthwksGroup()
Dim ctrl As Controls
Dim CatchmentCount As Integer

CatchmentCount = 0
For Each ctl In frmUSLE.Controls
If TypeName(ctrl) = "TextBox" Then
If ctrl.Name = "txtEarthwks" & CatchmentCount Then
CatchmentCount = CatchmentCount + 1
ReDim Preserve TextBoxes(1 To CatchmentCount)
Set TextBoxes(CatchmentCount).txtEarthwksGroup = ctrl
End If
End If
Next ctrl
End Sub


Sub USLE()
RuntxtEarthwksGroup
' Show the userform frmUSLE
frmUSLE.Show
End Sub

"Jon Peltier" wrote:

John Walkenbach shows here how to do this for buttons. Same idea,

http://www.j-walk.com/ss/excel/tips/tip44.htm

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Sam Kuo" wrote in message
...
Hi all,

I have 10 textboxes (namely txtPost1, txtPost2, ..., txtPost10) which I
want
to assign the event procedure below to. I just wonder if there might be
a
short way to do than copy and paste the code 10 times?

Private Sub txtPost1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update work duration (by calling macro UpdateDuration)
UpdateDuration ("Post")
End Sub







Peter T

Shorter way to create multiple identical Event Procedures?
 
You do need to qualify "TextBox" with MSForms as Jon suggests. However even
after doing that there are four textbox events that are not exposed to
WithEvents
Enter, Exit, BeforeUpdate, AfterUpdate

So you may have to think a different approach for your Exit event.

Also, after declaring your WithEvents object in the class module, select
your object in the middle dropdown and required event(s) from the right
dropdown

Regards,
Peter T

"Sam Kuo" wrote in message
...
Sorry, some corrections, but error still occurs...

' Below is saved in Class1
'Compile error: Object does not source automation events --
Public WithEvents txtEarthwksGroup As TextBox

Private Sub txtEarthwksGroup_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update work duration (by calling sub UpdateDuration in Module1)
UpdateDuration ("Earthwks")
End Sub





Sam Kuo[_3_]

Shorter way to create multiple identical Event Procedures?
 
Thanks Jon.

Thanks Peter for your further help. But I'm not clear about "select object
in the middle dropdown and required event(s) from the right dropdown". Where
are the middle and right dropdowns?

Sam


"Peter T" wrote:

You do need to qualify "TextBox" with MSForms as Jon suggests. However even
after doing that there are four textbox events that are not exposed to
WithEvents
Enter, Exit, BeforeUpdate, AfterUpdate

So you may have to think a different approach for your Exit event.

Also, after declaring your WithEvents object in the class module, select
your object in the middle dropdown and required event(s) from the right
dropdown

Regards,
Peter T

"Sam Kuo" wrote in message
...
Sorry, some corrections, but error still occurs...

' Below is saved in Class1
'Compile error: Object does not source automation events --
Public WithEvents txtEarthwksGroup As TextBox

Private Sub txtEarthwksGroup_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Update work duration (by calling sub UpdateDuration in Module1)
UpdateDuration ("Earthwks")
End Sub






Jon Peltier

Shorter way to create multiple identical Event Procedures?
 
The left and right dropdowns (no middle) are in the top section of the class
module code window.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"Sam Kuo" wrote in message
...
Thanks Jon.

Thanks Peter for your further help. But I'm not clear about "select object
in the middle dropdown and required event(s) from the right dropdown".
Where
are the middle and right dropdowns?

Sam


"Peter T" wrote:

You do need to qualify "TextBox" with MSForms as Jon suggests. However
even
after doing that there are four textbox events that are not exposed to
WithEvents
Enter, Exit, BeforeUpdate, AfterUpdate

So you may have to think a different approach for your Exit event.

Also, after declaring your WithEvents object in the class module, select
your object in the middle dropdown and required event(s) from the right
dropdown

Regards,
Peter T

"Sam Kuo" wrote in message
...
Sorry, some corrections, but error still occurs...

' Below is saved in Class1
'Compile error: Object does not source automation events --
Public WithEvents txtEarthwksGroup As TextBox

Private Sub txtEarthwksGroup_Exit(ByVal Cancel As
MSForms.ReturnBoolean)
' Update work duration (by calling sub UpdateDuration in Module1)
UpdateDuration ("Earthwks")
End Sub









All times are GMT +1. The time now is 02:08 PM.

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