ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Reducing Code (https://www.excelbanter.com/excel-programming/279533-reducing-code.html)

Garry Jones

Reducing Code
 
I have hundreds of text boxes on a user form.

For each text box I need to run the following traps on Exit and
BeforeUpdate to check user input and reset to old input if new input
does not meet requirements. I know I can run these traps as private
subs. By returning True or False I can trap the user or allow user to
continue inputting in the next text box.

This all works..
________

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Chkinp(TextBox1)
End Sub
_________

________

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Chkinp(TextBox1)
End Sub
________

Must I duplicate the code for every TextBox? Or is a way to use "For
Each" or a variable? If this code exists where would I put this so it
would be valid for the entire user form. (ie without creating two new
Private Subs for every text box).

Garry Jones
Sweden

Tom Ogilvy

Reducing Code
 
For each textbox, for the exit and beforeupdate events, you would need
separate events for each textbox.

--
Regards,
Tom Ogilvy

"Garry Jones" wrote in message
...
I have hundreds of text boxes on a user form.

For each text box I need to run the following traps on Exit and
BeforeUpdate to check user input and reset to old input if new input
does not meet requirements. I know I can run these traps as private
subs. By returning True or False I can trap the user or allow user to
continue inputting in the next text box.

This all works..
________

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Chkinp(TextBox1)
End Sub
_________

________

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Chkinp(TextBox1)
End Sub
________

Must I duplicate the code for every TextBox? Or is a way to use "For
Each" or a variable? If this code exists where would I put this so it
would be valid for the entire user form. (ie without creating two new
Private Subs for every text box).

Garry Jones
Sweden





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

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