VB for Whole Number result after divide

Rocky0201

Active Member
Joined
Aug 20, 2009
Messages
278
Hi...

I need to divide a value in an integer and receive the result as a whole number... not with the fraction.

If a divide 7 by 2, I need the result in my int to be 3 not 3.5. I tried using Rnd but got all fraction as a result.

Thanks.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try like this

Code:
Sub test()
Dim x As Integer
x = Int(7 / 2)
MsgBox x
End Sub
 
Upvote 0
Just to elaborate on VoG's code..

He set 'x' as an integer variable so that 'x' can only take integer part of a result.
So, any values will become truncated when using Int method where it could be seen in the code.

So, x = Int (7/2)
would become x = Int (3.5)
Then Int will truncate everything after the decimal giving you
x = 3
 
Upvote 0
On the other hand, you would use float or double datatype (just like integers) when you want to store decimal places/fractions in a variable.
 
Upvote 0
Code:
Sub G()
    Const i As Integer = 7
    MsgBox Fix(i / 2)
End Sub
 
Upvote 0
Hi

Remark: check also the integer division operator "\"

Ex.:

Code:
Sub Test()
    Const i As Long = 26
    MsgBox i \ 7
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,702
Members
452,938
Latest member
babeneker

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