Private Sub TextBox1_Change()
If TypeName(Me.ActiveControl) = "TextBox" Then
With Me.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Then
MsgBox "Sorry, only numbers allowed"
.Value = vbNullString
End If
End With
End If
End Sub
Try putting the code below (from Ozgrid) in the Userforms module.
Code:Private Sub TextBox1_Change() If TypeName(Me.ActiveControl) = "TextBox" Then With Me.ActiveControl If Not IsNumeric(.Value) And .Value <> vbNullString Then MsgBox "Sorry, only numbers allowed" .Value = vbNullString End If End With End If End Sub
Dim LastPosition As Long
Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With TextBox1
' Digits Only
'If .Text Like "*[!0-9]*" Then
' Floating Point Numbers
If .Text Like "*[!0-9.]*" Or .Text Like "*.*.*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
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
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 59
' Allow digits - do nothing
Case 46
' Allow only one dot symbol
If InStr(ActiveControl, ".") Then KeyAscii = 0
Case Else
' Block others
KeyAscii = 0
End Select
End Sub
This code allows typing of digits & dot symbol and block others:
Rich (BB code):Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Select Case KeyAscii Case 48 To 59 ' Allow digits - do nothing Case 46 ' Allow only one dot symbol If InStr(ActiveControl, ".") Then KeyAscii = 0 Case Else ' Block others KeyAscii = 0 End Select End Sub
Private Sub TextBox1_Enter()
With New DataObject
.GetFromClipboard
.SetText Trim(Str(Val(.GetText)))
.PutInClipboard
End With
End Sub
To solve the pasting issue you can use this additional code:
Code:Private Sub TextBox1_Enter() With New DataObject .GetFromClipboard .SetText Trim(Str(Val(.GetText))) .PutInClipboard End With End Sub
You are quite welcome... I am glad you liked it.Nice code Rick, thanks for sharing!
No problem... that is one of the nice things about the structure of my code, it is easy to modify for different conditions. Here is the code with two new color sections added to allow the digits only (purple) and the floating point numbers (green) to have a leading plus or minus sign (as before, uncomment the one you want and make sure the others are commented out)...BTW, supporting of negative numbers would be useful too
Dim LastPosition As Long
Private Sub TextBox1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With TextBox1
' Digits Only (no plus or minus)
'If .Text Like "*[!0-9]*" Then
' Digits Only (plus or minus allowed)
'If .Text Like "*[!0-9+-]*" Or .Text Like "?*[+-]*" Then
' Floating Point Numbers (no plus or minus)
'If .Text Like "*[!0-9.]*" Or .Text Like "*.*.*" Then
' Floating Point Numbers (plus or minus allowed)
If .Text Like "*[!0-9.+-]*" Or .Text Like "?*[+-]*" Or .Text Like "*.*.*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
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
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
With TextBox1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub