ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Which textbox have focus? (https://www.excelbanter.com/excel-programming/275443-re-textbox-have-focus.html)

Tom Ogilvy

Which textbox have focus?
 
Private Sub TextBox1_Change()
convert_to_integer TextBox1
End Sub

Public Sub convert_to_integer(tbox as msforms.textbox)
If IsNumeric(Right(tbox.text, 1)) = False Then
Application.SendKeys "{BACKSPACE}"
End If

End Sub


But it would be better to use the keypress event.

See this post by Harald Staff (actually look at the thead)

http://groups.google.com/groups?thre...%40tkmsftngp05

You can use a single event to handle multiple textboxes using this technique
by John Walkenbach

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

It is written for commandbuttons, but works for textboxes and other msforms
2.0 controls as well.

--
Regards,
Tom Ogilvy




"kafka" wrote in message
...
Hi all.

I have 25 textbox in my user form and all textbox values must be
integer.
so i do it with this code

Private Sub TextBox1_Change()
If IsNumeric(Right(TextBox1, 1)) = False Then
Application.SendKeys "{BACKSPACE}"
End If
End Sub

As i said i have 25 textbox it takes a long time to write this code for
every text box on the user form.

Now i am trying make a procedure search all the texboxes which have
focus and make the above code.

For example

Private Sub TextBox1_Change()
convert_to_integer
End Sub


Public Sub convert_to_integer()
Dim ctrl As Control
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "TextBox" Then
*If ctrl.SetFocus = True Then*
If IsNumeric(Right(ctrl, 1)) = False Then
Application.SendKeys "{BACKSPACE}"
End If
End If
End If
Next ctrl
End Sub

but this code givinme error on bold line "*If ctrl.SetFocus = True
Then* "

So how can i find the focused text box .

Thanks in advance.





All times are GMT +1. The time now is 05:57 PM.

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