Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default Textbox values

I have a userform with some text boxes on them. I need to find a way to
restrict what the user can enter to integer values only. Any suggestions?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Textbox values

This'll stop the typing of anything but digits:

Option Explicit
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
'numbers
Case Asc("0") To Asc("9")
'ok
Case Else
KeyAscii = 0
Beep
End Select
End Sub

Brad wrote:

I have a userform with some text boxes on them. I need to find a way to
restrict what the user can enter to integer values only. Any suggestions?


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Textbox values

Copy/Paste the following code into the UserForm's code window (change the
TextBox1 references to the name of the TextBox the code is to apply to).
Each TextBox will an identical set of code... besides changing the TextBox1
names to your actual TextBox's name, you will also have to create a
LastPosition2, LastPosition3, etc. for the other TextBoxes and you will have
to change the LastPosition1 references to these new LastPositionX names for
each group of code.

' Last position flag for TextBox1
Dim LastPosition1 As Long

'********** Start Code For TextBox1 **********
Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With TextBox1
If .Text Like "*[!0-9]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition1
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Single, _
ByVal Y As Single)
With TextBox1
LastPosition1 = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
LastPosition1 = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
'********** End Code For TextBox1 **********

--
Rick (MVP - Excel)


"Brad" wrote in message
...
I have a userform with some text boxes on them. I need to find a way to
restrict what the user can enter to integer values only. Any suggestions?


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Textbox values

This'll stop the typing of anything but digits:

But, so the OP knows, it will not stop the pasting of non-digits into the
TextBox (see the code I posted elsewhere in this thread for a method that
will stop all attempts to enter non-digits, whether typed or pasted).

--
Rick (MVP - Excel)


"Dave Peterson" wrote in message
...
This'll stop the typing of anything but digits:

Option Explicit
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
'numbers
Case Asc("0") To Asc("9")
'ok
Case Else
KeyAscii = 0
Beep
End Select
End Sub

Brad wrote:

I have a userform with some text boxes on them. I need to find a way to
restrict what the user can enter to integer values only. Any suggestions?


--

Dave Peterson


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Textbox values

That's what I meant by typing <vbg.

But I could/should have been more explicit.

Rick Rothstein wrote:

This'll stop the typing of anything but digits:


But, so the OP knows, it will not stop the pasting of non-digits into the
TextBox (see the code I posted elsewhere in this thread for a method that
will stop all attempts to enter non-digits, whether typed or pasted).

--
Rick (MVP - Excel)

"Dave Peterson" wrote in message
...
This'll stop the typing of anything but digits:

Option Explicit
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
'numbers
Case Asc("0") To Asc("9")
'ok
Case Else
KeyAscii = 0
Beep
End Select
End Sub

Brad wrote:

I have a userform with some text boxes on them. I need to find a way to
restrict what the user can enter to integer values only. Any suggestions?


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
TextBox Values Abdul[_2_] Excel Programming 1 August 1st 06 11:35 AM
Textbox values browie Excel Programming 3 June 20th 05 02:52 PM
How do I add TextBox.values? WTG Excel Programming 4 February 28th 05 09:38 AM
How do I add TextBox.values? WTG Excel Discussion (Misc queries) 1 February 27th 05 08:25 PM
How do I add TextBox.values? WTG Excel Worksheet Functions 1 February 27th 05 08:22 PM


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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"