ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Pad with leading zeros? (https://www.excelbanter.com/excel-programming/296088-pad-leading-zeros.html)

43fan

Pad with leading zeros?
 
I have a string that's made up of concatenating other variables together,
one of which is a numerical value. I want to put leading zeros on the
numeric value, up to a full string length of 3.

Ex:
A = Y (string)
B = 1234 (string)
C = 1 (numeric)
A B C
Final string would look like this: Y1234.001

Help???

Thanks!
Shawn



Jake Marx[_3_]

Pad with leading zeros?
 
Hi Shawn,

Is this what you are looking for, or are you looking for something
VBA-related?

=A1&A2&TEXT(A3/1000,".000")

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


43fan wrote:
I have a string that's made up of concatenating other variables
together, one of which is a numerical value. I want to put leading
zeros on the numeric value, up to a full string length of 3.

Ex:
A = Y (string)
B = 1234 (string)
C = 1 (numeric)
A B C
Final string would look like this: Y1234.001

Help???

Thanks!
Shawn



mark

Pad with leading zeros?
 
Format(C,"000")


-----Original Message-----
Ex:
A = Y (string)
B = 1234 (string)
C = 1 (numeric)
A

B C
Final string would look like this: Y1234.001



43fan

Pad with leading zeros?
 
Jake,

Thanks for the quick reply, but yes, something VBA related. I'm printing a
heading on a report that will be the concatenation of several fields like I
said, and one of those fields needs to have leading zeroes.

I'm wondering though, could I use something like this?

newvar = A + B + str(C/1000)?

C is always an integer, rarely goes higher than 4, occasionally to 6, but I
suppose could go as high as 15-20, but that would still end up with it being
..020 once divided by 1000. Would the str() function convert that to ".020"?

Gonna give it a try and see what happens. :) Thanks again!
"Jake Marx" wrote in message
...
Hi Shawn,

Is this what you are looking for, or are you looking for something
VBA-related?

=A1&A2&TEXT(A3/1000,".000")

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


43fan wrote:
I have a string that's made up of concatenating other variables
together, one of which is a numerical value. I want to put leading
zeros on the numeric value, up to a full string length of 3.

Ex:
A = Y (string)
B = 1234 (string)
C = 1 (numeric)
A B C
Final string would look like this: Y1234.001

Help???

Thanks!
Shawn





Jake Marx[_3_]

Pad with leading zeros?
 
43fan wrote:
Thanks for the quick reply, but yes, something VBA related. I'm
printing a heading on a report that will be the concatenation of
several fields like I said, and one of those fields needs to have
leading zeroes.

I'm wondering though, could I use something like this?

newvar = A + B + str(C/1000)?


When concatenating Strings, you should use the & operator. The Str function
will work, but Format gives you more control over the output.

As Mark suggested, you can use the Format function:

Dim sNewVar As String

sNewVar = Range("A1").Value & Range("A2").Value & _
Format$(Range("A3").Value/1000, ".000")

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Bob Phillips[_6_]

Pad with leading zeros?
 
Jake,

I agree you should use & for annotation, but + works.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Jake Marx" wrote in message
...
43fan wrote:
Thanks for the quick reply, but yes, something VBA related. I'm
printing a heading on a report that will be the concatenation of
several fields like I said, and one of those fields needs to have
leading zeroes.

I'm wondering though, could I use something like this?

newvar = A + B + str(C/1000)?


When concatenating Strings, you should use the & operator. The Str

function
will work, but Format gives you more control over the output.

As Mark suggested, you can use the Format function:

Dim sNewVar As String

sNewVar = Range("A1").Value & Range("A2").Value & _
Format$(Range("A3").Value/1000, ".000")

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]





All times are GMT +1. The time now is 04:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com