Same event procedure for multiple controls

  • Thread starter Thread starter Mikhail
  • Start date Start date
M

Mikhail

I have a userform with 18 TextBoxes. Their values are read from a worksheet.
On KeyPress event I write data back to worksheet.
Is it possible to write a procedure which accepts textbox control as
parameter as runs the same On_KeyPress sub for all 18 TextBoxes?
Or may be when KeyPress event is occurred on a form, is it possible to
determine which TextBox is active and do necessary actions on this TextBox?

Thanks in advance, Mike510
 
You can use the technique documented by John Walkenbach:

http://j-walk.com/ss/excel/tips/tip44.htm
Handle Multiple UserForm Buttons With One Subroutine

This uses commandbuttons, but can be adapted for textboxes. Note that only
the events specific to the control can be handled this way. Events, such as
the exit event, inherited from the control object, can not be handled this
way.

It sounds like your textboxes are linked to cells, however. The link is a
two way street - if you make an entry in the textbox, it is also placed in
the linked cell. - make sure you are not fighting yourself, so to speak.
 
Back
Top