Format a UserForm TextBox

Jockster

Board Regular
Joined
Jan 16, 2009
Messages
88
Hi,
The following will pad out an entry with zeros to ensure four digits are displayed:
Code:
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  TextBox2.Value = Format(TextBox2.Value, "0000")
End Sub
So, if a user enters 53 for instance, 0053 will be displayed.

How would I adapt this to take into account when a letter suffix is added to a number? For example, if a user enters 53a I would like 0053a to be returned.
There will never be greater than four digits but occasionally there will be a letter suffix.
Thanks.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Sandeep Warrier

Well-known Member
Joined
Oct 31, 2008
Messages
2,677
Not tested... but try

Code:
Dim Num

Num = TextBox2.Value
If IsNumeric(Right$(Num,1)) Then
    TextBox2.Value = Format$(Val(Left$(Num,Len(Num)-1)),"0000")
Else
    TextBox2.Value = Format$(Val(Left$(Num,Len(Num)-1)),"0000") & Right$(Num,1)
End If
 
Upvote 0

Forum statistics

Threads
1,190,694
Messages
5,982,337
Members
439,774
Latest member
InfinityMrsn

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
Top