Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format numbers

Status
Not open for further replies.

kadara

Programmer
Mar 2, 2012
26
DE
Hi,

I would like to use numbers in 3-digit format: for example 1 to 001, 2 to 002, 10 to 010, 11 to 011 and so on.
In details: I would like to get the following string format W20HYaaa where aaa is a number between 1 and 255.
How could I do that?
 
yourString = "W20HY" & Right("000" & yourNumber, 3)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I test it with the following code:
Code:
aaa = 12
number = right("000" + aaa, 3)

MsgBox number

but don't work (the result is 12 instead of 012).
 
Did you try my suggestion ?
Note the & instead of +

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks.

The
Code:
yourString = "W20HY" & Right("000" & yourNumber, 3)
code works well.
 
>+ is for numbers

Not quite ... + is overloaded. If both operands are strings it will concatenate just like &
 
Sorry, I must be missing your point. I quite clearly said "If both operands are strings". If one isn't a string, such as null, then it won't concatenate like & (which isn't overloaded)
 
Heck, even if you ignore the ability to concatenate, + is overloaded. It knows how to add integers, longs, singles, and doubles. You may argue that yes, that's what addition is, covers all of those. But VBA's + operator works differently depending on the type of the operands, which is typical of overloading (and concatenation is just an version of that)

Indeed most of the maths operators can be considered overloaded. For example

Msgbox 256 * 256

will error because the operand sees two integer operands and so does an integer calculation, and overflows. Change either operand to a long (e.g. MsgBox 256 * 256&), and the operator does a long calculation instead. This is a classic example of what the object-orientated world sometimes calls ad hoc polymorphism.

Is it bad practice? I'd suggest not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top