Monday, August 10, 2020

Excel VBA RoundDown and RoundUp functions

Function TestMin(DMin As Double)
        If DMin < 100 Then
            DMin = WorksheetFunction.RoundDown(DMin, 0)
        ElseIf DMin < 1000 Then
            DMin = WorksheetFunction.RoundDown(DMin, -1)
        ElseIf DMin < 10000 Then
            DMin = WorksheetFunction.RoundDown(DMin, -2)
        ElseIf DMin < 100000 Then
            DMin = WorksheetFunction.RoundDown(DMin, -3)
        ElseIf DMin < 1000000 Then
            DMin = WorksheetFunction.RoundDown(DMin, -4)
        End If
        TestMin = DMin
End Function
Function TestMax(DMax As Double)
        If DMax < 100 Then
            DMax = WorksheetFunction.RoundUp(DMax, 0)
        ElseIf DMax < 1000 Then
            DMax = WorksheetFunction.RoundUp(DMax, -1)
        ElseIf DMax < 10000 Then
            DMax = WorksheetFunction.RoundUp(DMax, -2)
        ElseIf DMax < 100000 Then
            DMax = WorksheetFunction.RoundUp(DMax, -3)
        ElseIf DMax < 1000000 Then
            DMax = WorksheetFunction.RoundUp(DMax, -4)
        End If
        TestMax = DMax
End Function
Sub test()
    MsgBox TestMin(87789.89)
    MsgBox TestMax(97979.89)
End Sub

No comments:

Post a Comment

Hot Topics