Textboxes in Userforms

Dazed_

New Member
Joined
Feb 16, 2002
Messages
5
I'm using a userform for data entry, which includes a textbox for a numeric code. My problem is that the textbox automatically kills all leading zeros... ie... 0104 will become 104. However i need these leading zeros as the are part of the code. Can anyone help me with this?

Cheers...

Daniel
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi Daniel,

I hope you are not too dazed.

Actually, you must set up the textbox for string input, because numerically there is no difference between 0104 and 104--they are both the value one hundred four. What I would recomend is that right after textbox entry you check the string using the VBA IsNumeric function to ensure that the string represents a legal numeric value. But from that point on, since you want 0104 to be distinct from 104 you must deal with it as a string. The second you convert the code to any numeric type (Integer, Long, Single, Double, etc.) it will lose its "distinctness."
 
Upvote 0
You could also try with this code (Which converts the text to a string)

<pre>
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
TextBox1 = Format(TextBox1, "0000")
End Sub
</pre>
 
Upvote 0
Thanks for your suggestions guys...

Just wondering, how would i set a textbox for string entry only, as opposed to numeric? It there an option somewhere that i can select?

Cheers...

Dan
 
Upvote 0
Don't worry.... i've sorted it... i just set the cells i was putting the data into to be text format and its solved all my worries...

:)

Cheers
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,707
Members
448,981
Latest member
recon11bucks

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