#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default textbox

Is there an easy way of only allowing numbers and one
decimal point to be entered in a textbox?

I've found a rather long winded way of solving the number
bit (see code below) but checking each previously entered
character to see if its a decimal point has proved to be a
problem; and there must be a simpler way!

Private Sub TextBox1_Change()
If TextBox1.Text < "" Then
If Asc(Right(TextBox1.Text, 1)) < 48 _
Or Asc(Right(TextBox1.Text, 1)) 57 Then
TextBox1.Text = _
Left(TextBox1.Text, TextBox1.TextLength - 1)
End If
End If
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default textbox

tamsin,

Use this event if you require a decimal point to be entered:

Private Sub TextBox1_AfterUpdate()
If InStr(1, TextBox1.Text, ".") 0 Then Exit Sub
MsgBox "You need to enter a decimal point."
UserForm1.TextBox1.SetFocus
End Sub

Use this event to allow only numbers to be entered:
Private Sub TextBox1_Change()
Dim myVal As Long
If TextBox1.Text < "" Then
On Error GoTo Changeback
myVal = CLng(TextBox1.Text)
End If
Exit Sub
Changeback:
TextBox1.Text = _
Left(TextBox1.Text, TextBox1.TextLength - 1)
End Sub

HTH,
Bernie
MS Excel MVP

"tamsin" wrote in message
...
Is there an easy way of only allowing numbers and one
decimal point to be entered in a textbox?

I've found a rather long winded way of solving the number
bit (see code below) but checking each previously entered
character to see if its a decimal point has proved to be a
problem; and there must be a simpler way!

Private Sub TextBox1_Change()
If TextBox1.Text < "" Then
If Asc(Right(TextBox1.Text, 1)) < 48 _
Or Asc(Right(TextBox1.Text, 1)) 57 Then
TextBox1.Text = _
Left(TextBox1.Text, TextBox1.TextLength - 1)
End If
End If
End Sub



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
Calculate Textbox value based on another textbox value.doc Tdungate Excel Discussion (Misc queries) 1 February 12th 09 07:11 PM
Calculate Textbox value based on another textbox value Tdungate Excel Discussion (Misc queries) 0 February 12th 09 07:03 PM
Textbox majikman[_15_] Excel Programming 1 May 13th 04 04:56 AM
to textbox Mark[_17_] Excel Programming 2 November 20th 03 08:46 AM
UserForm TextBox to ActiveSheet TextBox over 256 characters Dan E[_2_] Excel Programming 1 July 28th 03 07:36 PM


All times are GMT +1. The time now is 08:35 AM.

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

About Us

"It's about Microsoft Excel"