Floor function in VBA code

Lupison

Board Regular
Joined
Jun 14, 2006
Messages
50
I found Round() but I can't find a Floor() math function within the VBA code side of the excel.

For those not math inclined, Floor(5.65) = 5, whereas Round(5.65) = 6.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi,

perhaps some vba-mathexpert will know if it exists
this is my workaround
Code:
Sub test()
Dim result As Long

Const i = 5.65

result = Int(i) - 1 * (Int(i) > i)
MsgBox result

End Sub
kind regards,
Erik
 
Upvote 0
Why not use Application.WorksheetFunction.Floor?
 
Upvote 0
Erik

I don't really know if it would be slower, I can't recall seeing a discussion on the matter anywhere, I'll perhaps have a look and post any interesting links if I find any.

Just seems to me to make sense to use what's available rather than trying to recreate something.:)

Also I'm not sure exactly how you would go about testing the efficiency - perhaps some sort of loop?
 
Upvote 0
Code:
Sub test()
Dim result As Long
Dim starttime As Double
Dim i As Long

starttime = Timer
For i = 1 To 3000000
'result = Int(5.65) - 1 * (Int(5.65) > i)
result = Application.WorksheetFunction.Floor(5.65, 1)
Next i
MsgBox Timer - starttime
End Sub
 
Upvote 0
Erik

Just tried this code, based on yours.

Here are the result.

42.4518749999988
1.86906250000175

So it appears you're right about the speed issue.:)

By the way I reversed the order of the code just to make sure I wasn't seeing things and got a similar result.

Code:
Sub test()
Dim result As Long
Dim starttime As Double
Dim i As Long

    starttime = Timer
    For i = 1 To 3000000
        'result = Int(5.65) - 1 * (Int(5.65) > i)
        result = Application.WorksheetFunction.Floor(5.65, 1)
    Next i
    Debug.Print Timer - starttime
    starttime = Timer
    
    For i = 1 To 3000000
        result = Int(5.65) - 1 * (Int(5.65) > i)
        'result = Application.WorksheetFunction.Floor(5.65, 1)
    Next i
    Debug.Print Timer - starttime
End Sub
 
Upvote 0
of course we're talking about 3 million operations
would be about 45 entire columns of data to convert
yeah, why not :LOL:
 
Upvote 0
Floor(x) is defined as the largest integer not greater than x.

Int(x) gives that result.

I found Round() but I can't find a Floor() math function within the VBA code side of the excel.

For those not math inclined, Floor(5.65) = 5, whereas Round(5.65) = 6.
 
Upvote 0
:oops:

OK, tusharm,
you were thinking clearly

result = Int(5.65) '''''''''''''''' - 1 * (Int(5.65) > i)
result = Int(5.65) '''''''''''''''' - 0 :oops:
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,243
Members
449,075
Latest member
staticfluids

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