View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.newusers
LurfysMa LurfysMa is offline
external usenet poster
 
Posts: 61
Default Custom format macro?

Can I write a macro to format a number?

I have a bunch of cells containing time intervals stored as floating
point numbers in units of days. These are not dates, but are stored in
the same format. Some examples:

1 1 day
2.5 2.5 days or 2 days, 12 hours
0.125 3 hours
0.020833 30 minutes

and so on.

These values are calculated. I would like to display them as

nnn.n u

where "nnn.n" is a floating point number formatted to 1 decimal place
and "u" is a character indicating the units (Y=years, D=days, H=hours,
M=minutes, S=seconds). The macro will chose the largest units that
have at least 1 digit on the left of the decimal place. For example:

1 1.0D
2.5 2.5D
0.125 3.0H
0.020833 30.0M
730.5 1.5Y

I already have the macro code that I wrote for a VB project. My
problem is how to get Excel to use it to format the value in the cell.

I know I call pass the value to the macro and have it return a string
(text) result, but then that result is not available as a number in
other calculations. I'd like to keep the value in the cell as a number
and just apply the macro for format it.

--