Format number digit

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
209
I Use zeros to format the number of digits i want when generating numbers, i.e 000 will display 054, 001, etc.
how do i code it on a userform to just type the number of digits i want directly instead of using zeros, i.e typing 5 in a textbox and it will generate numbers like 00045,00004,00569, etc?
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,296
Try this in userform module

Code:
Private Sub [COLOR=#ff0000]TextBox1[/COLOR]_Change()
    [COLOR=#ff0000]TextBox1[/COLOR].Value = Format([COLOR=#ff0000]TextBox1[/COLOR].Value, "00000")
End Sub
 

yinkajewole

Board Regular
Joined
Nov 23, 2018
Messages
209
This is not what I mean/want.

This is what I'm trying to say
There will be a textbox1 for the numbers and textbox2 for the digit.
So the user will just input 45 into textbox1 with he inputs 6 in textbox2.
Hence, the result will be 000045
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,296
this requires that textbox1 is completed first


Code:
Private Sub TextBox2_Change()
    On Error Resume Next
    TextBox2.Value = Abs(CInt(TextBox2.Value))
    If Err.Number > 0 Then
        TextBox2.Value = 0
        Exit Sub
    Else
        TextBox1.Value = Format(TextBox1.Value, String(TextBox2.Value, "0"))
    End If
End Sub
code not required for textbox1 but you should validate textbox1 value to ensure numeric
 
Last edited:

Forum statistics

Threads
1,078,486
Messages
5,340,616
Members
399,387
Latest member
amrita34

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top