Overflow error at variable assignment using integers and date values

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
Greetings,

I am running into an overflow error and seeking assistance in how to handle. The line giving me trouble is below:

Code:
B = (wsPA.Cells(K, "E").Value - wsPA.Cells(RowLoop1, "E").Value) / (wsPA.Cells(K, "A").Value - wsPA.Cells(RowLoop1, "A").Value)

The E variables are integer values, while the "A" columns are DATE values of the type. The B variable is an integer value.

I think my question is how to maintain the B variable as an integer while including date variables of the MMDDYYYY type as a basis for the slope value. I would like to find the number of days between the date values as the denominator of the equation.

Thanks!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi SBF12345,

An interger variable can only hold numbers in the range -32,768 to 32,767. Try converting your integer variables to long.

A great overview of variables can be found here.

Regards,

Robert
 
Upvote 0
Change the Integer declarations to Long.

The range of Integer variables is –32,768 to 32,767, which is easily exceeded when working with dates - for example 2008-01-01 (today's date) is stored as the integer value 43101.
 
Upvote 0
Hmmm...I changed the B variable to Long but am still receiving the overflow error. I have the dates stored as "MM/DD/YYYY" in the cells on the sheet. I'd prefer to keep them in this format if possible. Could the date format be a contributing factor?
 
Upvote 0
Could the date format be a contributing factor?

No, I wouldn't think so :confused:

Debug each part of the formula to see / verify what each component of your formula is returning i.e.

Code:
Debug.Print wsPA.Cells(K, "E").Value
Debug.Print wsPA.Cells(RowLoop1, "E").Value
Debug.Print wsPA.Cells(K, "A").Value
Debug.Print wsPA.Cells(RowLoop1, "A").Value

and then see if you can do the formula with the given results.
 
Upvote 0
Hmmm...I changed the B variable to Long but am still receiving the overflow error.

My guess is that RowLoop1 is exceeding 32,767.

As Norie and Trebor76 no doubt intended: Convert all your Integers to Longs (and always use Long - there's no point in using the Integer Data Type in VBA).

I'm curious though ... why are you constraining B to be integer when presumably the slope won't always be integer?
 
Upvote 0
I changed all my integer variables to longs.

The problem was that the denominator value for the variable B was "0" and thus creating a problem, cannot divide by zero. I also had to change some of the variables around in the script, but this was unrelated.

On the record, I used Add watch to was the specific row values to determine their values at the moment of the error.
 
Upvote 0

Forum statistics

Threads
1,214,851
Messages
6,121,931
Members
449,056
Latest member
denissimo

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