No reason to be sorry (but there is stilll a problem).
I examined your function, putting test values in column A and the function
call in Column B:
99999 273.8Y
0.01 14.4M
0.9 21.6H
0.99 1.0D
500 1.4Y
1000 2.7Y
Note that .99 results in 1 Day rather than 23.8 Hours
Here is the event code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A:A")) Is Nothing Then
Exit Sub
End If
Const TSYear As Double = 365.25 ' 1 year
Const TSDay As Double = 1 ' 1 day
Const TSHour As Double = TSDay / 24 ' 1 hour
Const TSMin As Double = TSHour / 60 ' 1 minute
Const TSSec As Double = TSMin / 60 ' 1 second
v = Target.Value
dq = Chr(34) ' double quote
Select Case v
Case Is > TSYear
v = v / TSYear
s = "Y"
Case Is > TSDay
v = v / TSDay
s = "D"
Case Is > TSHour
v = v / TSHour
s = "H"
Case Is > TSMin
v = v / TSMin
s = "M"
Case Else
s = "S"
End Select
Application.EnableEvents = False
Target.Offset(0, 1).Value = v
Target.Offset(0, 1).NumberFormat = "0.0" & dq & s & dq
Application.EnableEvents = True
End Sub
Once installed, type a value in column A and the result will appear in
column B complete with formatting. Because it is event code, it is very easy
to install and automatic to use:
1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it.
To remove the macro:
1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
--
Gary''s Student - gsnu200732
"LurfysMa" wrote:
> The function I posted won't work. It has a bug. Here is the corrected
> function. Sorry.
>
>
> '************************************************************************
> ' Format Interval Function
> '
> ' Formats a time interval as nnn.nu
> '
> ' nnn.n = interval to 1 decimal place
> ' u = units: Y=years, D=days, H=hours, M=minutes, S=seconds
> '
> ' Syntax: y = FmtInt(interval)
> '
> ' Note: The format function
>
> Public Function FmtInt(ByVal interval As Double) As String
>
> Const TSYear As Double = 365.25 ' 1 year
> Const TSDay As Double = 1 ' 1 day
> Const TSHour As Double = TSDay / 24 ' 1 hour
> Const TSMin As Double = TSHour / 60 ' 1 minute
> Const TSSec As Double = TSMin / 60 ' 1 second
> ' Note: if a variable number of decimal places are needed,
> ' this constant must be generated dynamically
> Const FmtPat As String = "0.0" ' The format function pattern
>
> If Format(interval / TSYear, FmtPat) >= 1 Then
> FmtInt = Format(interval / TSYear, FmtPat) & "Y"
> ElseIf Format(interval, FmtPat) >= 1 Then
> FmtInt = Format(interval, FmtPat) & "D"
> ElseIf Format(interval / TSHour, FmtPat) >= 1 Then
> FmtInt = Format(interval / TSHour, FmtPat) & "H"
> ElseIf Format(interval / TSMin, FmtPat) >= 1 Then
> FmtInt = Format(interval / TSMin, FmtPat) & "M"
> Else
> FmtInt = Format(interval / TSSec, FmtPat) & "S"
> End If
>
> End Function
>
> --
>