Large values not being handled by UDF

Juggler_IN

Active Member
Joined
Nov 19, 2014
Messages
349
Office Version
  1. 2003 or older
Platform
  1. Windows
The below UDF is executing for 9 digit values and fails beyond 9 digits.

I have tried decimal data type but no luck. How I can make this UDF take higher digit values.

It works for x = 127,098,075 but fails for x = 50,428,677,591.

VBA Code:
Public Function Units(x As Long) As String

    Dim Seconds
    Dim Minutes
    Dim Hours
    Dim Days
    Dim DayString As String
    Dim HourString As String
    Dim MinuteString As String
    Dim SecondString As String

    Seconds = Int(x Mod 60)
    Minutes = Int(x \ 60 Mod 60)
    Hours = Int(x \ 3600 Mod 24)
    Days = Int(x \ 3600 \ 24)

    DayString = "d "
    HourString = "h "
    MinuteString = "m "
    SecondString = "s"

    Select Case Days
    Case 0
        Units = Format(Hours, "0") & HourString & _
                Format(Minutes, "0") & MinuteString & _
                Format(Seconds, "0") & SecondString
    Case Else
        Units = Days & DayString & _
                Format(Hours, "0") & HourString & Format _
                (Minutes, "00") & MinuteString & _
                Format(Seconds, "00") & SecondString
    End Select

End Function

Any possible tweak?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
The Long variable (which you have declared x as) has a limit of 2,147,483,647. Try declaring it as Double instead.
 
Upvote 0
Hi, try this code:
VBA Code:
Public Function Units(ByVal x As Double) As String
    Dim Seconds, Minutes, Hours, Days
    Seconds = FnMod(x, 60)
    Minutes = FnMod(x / 60#, 60)
    Hours = FnMod(x / 3600#, 24)
    Days = Fix(x / 86400#)
    Units = IIf(Days, Days & "d ", "") & _
            Format(Hours, "0") & "h " & _
            Format(Minutes, "00") & "m " & _
            Format(Seconds, "00") & "s"
End Function

Function FnMod(N As Double, Base As Long)
  FnMod = Fix(Fix(N) - Fix(N / Base) * Base)
End Function

Sub Test()
  Dim v As Double
  v = 60635462889#
  Debug.Print Units(v)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,453
Members
448,898
Latest member
drewmorgan128

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top