Integer rounding issue in VBA

Showard27

Board Regular
Joined
Sep 5, 2005
Messages
155
Hi all

I have come across a bit of a problem in my VBA coding which is throwing up a issue.

I take out a rounding error in my code with this statement, with EntryList declared as an Integer
Code:
EntryList = (LastCol - 10) / 2
so if LastCol is 71 then EntryList is set to 30 (ie 71-10 = 61 & 61 / 2 = 30.5 therefore the Integer is 30)....perfect
however
if LastCol is 73 then EntryList is being set to 32 (which indicates VBA/Excel is rounding up not taking the Integer from the Answer which is 31)
Have I found an anomaly, or am I missing something

Many thanks, am flummoxed
S
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi all

I have come across a bit of a problem in my VBA coding which is throwing up a issue.

I take out a rounding error in my code with this statement, with EntryList declared as an Integer
Code:
EntryList = (LastCol - 10) / 2
so if LastCol is 71 then EntryList is set to 30 (ie 71-10 = 61 & 61 / 2 = 30.5 therefore the Integer is 30)....perfect
however
if LastCol is 73 then EntryList is being set to 32 (which indicates VBA/Excel is rounding up not taking the Integer from the Answer which is 31)
Have I found an anomaly, or am I missing something
I am about to go to sleep, so I don't have time to write up a full response, but the problem is due to Banker's Rounding (you can Google it). VB using Banker's Rounding for all of its rounding with one exception... rounding done by the Format function is what most people consider normal rounding. Try changing the statement to this...

EntryList = Format((LastCol - 10)/2, "0")
 
Upvote 0
Many thanks

With a bit of investigation the Integer declaration does indeed round strangely sometimes up to the nearest even number, not sure if I would class this strictly as bankers rounding, however the Int function seems to operate properly. so I have modified as follows and it works.
Code:
Entrylist = Int(LastCol - 10) / 2
S
 
Upvote 0
Many thanks

With a bit of investigation the Integer declaration does indeed round strangely sometimes up to the nearest even number, not sure if I would class this strictly as bankers rounding, however the Int function seems to operate properly. so I have modified as follows and it works.
Code:
Entrylist = Int(LastCol - 10) / 2
The Int function does not actually round values, at least not in the context one normally associates with rounding. Int returns the first integer it comes to after rounding towards zero... that rounding takes place no matter what the fractional part is the number is... actual rounding, the kind I was referring to deals with how a number ending 5 rounds to the previous digit position. Banker's Rounding rounds to the even number whereas "normal rounding" rounds all 5 up to the digit that is one greater than the previous digit. And as I said, in all situations where actual rounding occurs within all VB functions and operations that force rounding, that rounding will be Banker's Rounding except for the Format function which performs "normal rounding" when it format pattern requires rounding to take place.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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