ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Value padding in VBA (https://www.excelbanter.com/excel-programming/285405-value-padding-vba.html)

raj

Value padding in VBA
 
Hello. Please help me if possible.

I need a function that will take a value that may be 1 to
3 digits in length and 0 to 2 decimal places and pad it to
always have three digits and two decimals.

For example:

1 would be converted to 001.00
14 would be converted to 014.00
15.3 would be converted to 015.30
145 would be converted to 145.00
197.21 would remain 197.21

The variable(s) can be strings if needed, I can work with
any VBA compatible datatype.

Your example code to do this would be most appreciated.
Thanks in advance for your assistance.

Klaus[_4_]

Value padding in VBA
 
Try following:

Sub test()
OrigValue = 15.3
NewValue = Format(OrigValue, "000.00")
End Sub


Regards
Klaus

-----Original Message-----
Hello. Please help me if possible.

I need a function that will take a value that may be 1 to
3 digits in length and 0 to 2 decimal places and pad it

to
always have three digits and two decimals.

For example:

1 would be converted to 001.00
14 would be converted to 014.00
15.3 would be converted to 015.30
145 would be converted to 145.00
197.21 would remain 197.21

The variable(s) can be strings if needed, I can work with
any VBA compatible datatype.

Your example code to do this would be most appreciated.
Thanks in advance for your assistance.
.


Greg Wilson[_4_]

Value padding in VBA
 
If your intention is only to display the figures in this
format on the worksheet then you can apply a custom format
to the worksheet cells that will accomplish this. Just
select the cells and then select Format|Cells|Number tab.
Then select Custom in the Category list and type 000.00 in
the Type window.

Regards,
Greg

-----Original Message-----
Hello. Please help me if possible.

I need a function that will take a value that may be 1 to
3 digits in length and 0 to 2 decimal places and pad it

to
always have three digits and two decimals.

For example:

1 would be converted to 001.00
14 would be converted to 014.00
15.3 would be converted to 015.30
145 would be converted to 145.00
197.21 would remain 197.21

The variable(s) can be strings if needed, I can work with
any VBA compatible datatype.

Your example code to do this would be most appreciated.
Thanks in advance for your assistance.
.



All times are GMT +1. The time now is 03:13 PM.

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