Class Module to include one decimal place in userform textboxes

dlee83

New Member
Joined
Mar 11, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have code in a class module that sets the textboxes to add only numbers:



Public WithEvents TextBoxClass As MSForms.TextBox

Private Sub TextBoxClass_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii

Case 48 To 57

Case Else

KeyAscii = 0
End Select
End Sub

Then seperate code in the userform for each individual textbox so the user can only input one decimal place:

Private Sub TextBox3_Change()

If IsNumeric(Right(Me.TextBox3.Text, 1)) = False And Right(Me.TextBox3.Text, 1) <> "." And Me.TextBox3.Text <> vbNullString Then
Me.TextBox3.Text = Left(Me.TextBox3.Text, Len(Me.TextBox3.Text) - 1)
Beep
ElseIf Right(Me.TextBox3.Text, 1) = "." Then
Dim CheckDecimal As Integer
Dim DecimalFound As Boolean
CheckDecimal = 1
DecimalFound = False
While CheckDecimal <= Len(Me.TextBox3.Text) - 1 And DecimalFound = False
If Right(Left(Me.TextBox3.Text, CheckDecimal), 1) = "." Then
DecimalFound = True
End If
CheckDecimal = CheckDecimal + 1
Wend
If DecimalFound = True Then
Me.TextBox3.Text = Left(Me.TextBox3.Text, Len(Me.TextBox3.Text) - 1)
Beep
End If
End If

End Sub


What can I put in a class module that negates the need for multiple textbox codes for one decimal place?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Make that code into a TextboxClass_Change event in your class, replacing Textbox3 with TextboxClass
 
Upvote 0
Solution
Make that code into a TextboxClass_Change event in your class, replacing Textbox3 with TextboxClass
Hi Rory,

That doesn't seem to work on it's own. I've put that code into the class and changed all the textbox3's. Do I need to do anything else?
Is there another way?
 
Upvote 0
Make that code into a TextboxClass_Change event in your class, replacing Textbox3 with TextboxClass
Hi Again,

My apologies. That does work. It was just that I needed to remove the keypress code which was preventing any other inputs anyway.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,838
Messages
6,174,942
Members
452,593
Latest member
Jason5710

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top