Question

I am trying to take a minute value (such as 3.83 minutes) and convert it to the hh:mm:ss time format (which I know is 0:03:50)

For some reason, the .NumberFormat as recorded from a macro isn't working and giving me a #VALUE! error.

    Function MINtoHMS(MIN)
    MIN = MIN / (24 * 60)
    MINtoHMS = MIN
    MINtoHMS.NumberFormat = "[h]:mm:ss;@"
    End Function
Was it helpful?

Solution

-Edit- To use as an add-in

Excel Add-In: http://www.filedropper.com/mintohms

Create a class module named SheetChangeHandler with the following code:

Option Explicit

Private WithEvents App As Application

Private Sub Class_Initialize()
    Set App = Application
End Sub

Private Sub App_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    On Error GoTo Err
    If InStr(Target.Formula, "=MINtoHMS") Then
        Target.NumberFormat = "hh:mm:ss"
    End If
    On Error GoTo 0
    Exit Sub
Err:
End Sub

Add a module with the following code:

Option Explicit

Public MySheetHandler As SheetChangeHandler

Sub Auto_Open()
   Set MySheetHandler = New SheetChangeHandler
End Sub

Function MINtoHMS(MIN)
    MIN = MIN / (24 * 60)
    MINtoHMS = MIN
End Function

Click File > Save As > Excel 97-2003 Add-In (*.xla) > Save

Click File > Options > Add-Ins

Click "Go..." next to Manage: Excel Add-ins

Check the box next to the add-in you just created

Click "OK"

OTHER TIPS

First, You cannot change the format of an Excel Cell through its Formula. A Cell's formula can only assign to the value of a cell (or range).

Secondly, You really ought to declare some data-types in your functions, which will prevent a host of mysterious errors and other strange results.

Something like this should be fine:

Function MINtoHMS(MIN As Double) As Date
    MIN = MIN / (24 * 60)
    MINtoHMS = MIN
End Function

The only way to absolutely control what is seen through a function, would be to return a formatted string instead, as Ripster shows in his/her answer.

Try these following methods.

DAYS = Format(Int([Expr3]/86400), "00") - will correctly display days
HOURS = Format(Int(([Expr3])/3600) - ((Int([Expr3]*86400)/3600), "00") - DOES NOT CORRECTLY display correct hours
HOURS = Format(Int([Expr3]/3600),"00") - What will display hours
MINUTES = Int(([Expr3]-(Int([Expr3]/3600)*3600))/60)
SECONDS = Format((([Expr3] Mod 60)),"00")

dTotalSeconds = DateDiff("S", Now(), dChristmasDate)
    iDays = Int(dTotalSeconds / 86400)
    iHours = Int((dTotalSeconds Mod 86400) / 3600)
    iMinutes = Int(((dTotalSeconds Mod 86400) Mod 3600) / 60)
    iSeconds = ((dTotalSeconds Mod 86400) Mod 3600) Mod 60

' By Using this function you can convert minutes to hh:mm:ss

    Public Function HMStoSec(strHMS As String) As Long
        HMStoSec = Split(strHMS, ":")(0) * 3600 + _
                   Split(strHMS, ":")(1) * 60 + _
                   Split(strHMS, ":")(2)
    End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top