Add trailing zeros in VBA

Flavien

Board Regular
Joined
Jan 8, 2023
Messages
78
Office Version
  1. 365
Platform
  1. Windows
Hello everybody,

I'm creating a userform that allows the user to enter up to 6 digits in the textbox1 and non-0 digits in the textbox2.
The contents of the 2 textboxes are concatenated to form a single 9-digit number.
example:
textbox1 = 123456 // textbox2 = 0 => 123456000
textbox1 = 123 // textbox2 = 1 => 123000001

With the worksheetfunction.text I can display 0s in front of the number entered in the textbox2 either 1 >> 001

Rich (BB code):
        Suffix2 = UserForm2.TextBox2.Value
        SufX2 = WorksheetFunction.Text(Suffix2, "000")

On the other hand, I can't add 0s after the numbers entered in the textbox1



Does anyone know how to do it? Thank you in advance for your help.
Rich (BB code):
    If UserForm2.TextBox2.Value = "" Then
        Suffix1 = UserForm2.TextBox1.Value
        SufX1 = WorksheetFunction.Text(Suffix1, "000000")
        Range("OF_Num").Value = UserForm2.TextBox1.Value & "000000"
    
    Else

        Suffix1 = UserForm2.TextBox1.Value
        SufX1 = WorksheetFunction.Text("000000", Suffix1)
        Suffix2 = UserForm2.TextBox2.Value
        SufX2 = WorksheetFunction.Text(Suffix2, "000")
    
        Range("OF_Num").Value = UserForm2.TextBox1.Value & SufX1 & UserForm2.TextBox2.Value & SufX2



    End If
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
If textbox1 always takes 6 digits (with zeros on the right) and textbox2 always takes 3 digits (with zeros on the left), to form a number with 9 digits:

PHP:
        Suffix1 = UserForm2.TextBox1.Value
        SufX1 =LEFT(Suffix1  & "000000", 6)
        Suffix2 = UserForm2.TextBox2.Value
        SufX2 =RIGHT("000000" & Suffix2, 3)
        Range("OF_Num").Value =SufX1 &  SufX2
 
Upvote 0
Solution

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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