How to replace other numbers?

Hi Experts,
can you help with macro that will replace the numbers? I control TextBox1 into which a user types a number between 100 and 9999999, I need to if the user writes three-digit number, the last digit replaced by zero. If you write a four-digit number, the last two digits were replaced by zeros. If you write a five-digit number, the last three digits replaced with zeros. If you write six-digit number, last four digits were replaced by zeros.
 Macro should start by clicking the CommandButton1.
 See attached sample.
 Many thanks in advance for any help. Remove-numbers.ppt
Thomas_MeyerAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
JSRWilsonConnect With a Mentor Commented:
Assuming this is Textbox1

Private Sub TextBox1_Change()
Dim L As Long
If Not IsNumeric(Me.TextBox1.Text) Then Me.TextBox1.Text = ""
L = Len(Me.TextBox1.Text)
If L > 2 Then Me.TextBox1.Text = Left(Me.TextBox1.Text, 2) & String(L - 2, "0")
End Sub

Open in new window

0
 
GrahamSkanConnect With a Mentor RetiredCommented:
Try this
Option Explicit


Private Sub CommandButton1_Click()
    Dim strNumbers As String
    Dim l As Integer
    
    strNumbers = CStr(TextBox1.Text)
    l = Len(strNumbers)
    TextBox1.Text = Left$(strNumbers, 2) & String(l - 2, "0")
End Sub

Open in new window

0
 
JSRWilsonCommented:
Note this does not require a click on the button but if you need that just transfer the code.
0
 
Thomas_MeyerAuthor Commented:
Perfect, thank you for all the help.
Best regards,
TM
0
All Courses

From novice to tech pro — start learning today.