How to replace other numbers?

Posted on 2011-10-08
Last Modified: 2012-05-12
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
Question by:Thomas_Meyer
    LVL 76

    Assisted Solution

    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

    LVL 23

    Accepted Solution

    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

    LVL 23

    Expert Comment

    Note this does not require a click on the button but if you need that just transfer the code.

    Author Closing Comment

    Perfect, thank you for all the help.
    Best regards,

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    My experience with Windows 10 over a one year period and suggestions for smooth operation
    This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
    The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now