getting an overflow error msg when running macro

kskinne

Well-known Member
Joined
Feb 17, 2002
Messages
1,267
Office Version
  1. 365
Platform
  1. Windows
when i run the following macro:

Code:
Sub test()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
For i = 9 To LastRow
Cells(i, 4).Value = Cells(i, 5).Value / Cells(i, 3).Value
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
i get the error message "Run-time error '6': Overflow"

when it hits the line:
Code:
Cells(i, 4).Value = Cells(i, 5).Value / Cells(i, 3).Value

the range of data i am running this macro on is about 20,000 rows of data that is pulled from an access database using a query. the code is erroring out on about row #11,802

anyone know how to avoid this?

thanks,
kevin
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You must Dim your counter variable i as long --

Dim i as Long

*Always* good practice... as is Option Explicit...
 
Upvote 0
Presumably that cell has a zero in it, check the cells with an If... Then statement around it before you do your calculation.
 
Upvote 0
still getting the error msg

I tried declaring my variable as Long and am still getting the same error message
 
Upvote 0
Chris, I'm not trying to argue, but plug 0 in A1, 1 in B1, and then run this code...

Code:
Sub checkoverflow()
Cells(1, 3).Value = Cells(1, 2).Value / Cells(1, 1).Value
End Sub

And then run this code and watch how the same "values" produce a different error message...

Code:
Sub checkoverflow2()

cell1 = Cells(1, 1).Value
cell2 = Cells(1, 2).Value
cell3 = cells2 / cell1

Cells(1, 3).Value = cell3

End Sub

Another wonderful perc of XL. :eek:
 
Upvote 0
Chris Howarth said:
I know, but make both A1 and B1 = 0 and you'll get the overflow error...

:oops: Sorry! Missed that part of the equation. My question remains, why does XL return a different error message for the same calculation (ie two codes listed above)?
 
Upvote 0
Hey kskinne, didn't forget about you. Try this and see if it works...

Code:
Sub test()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
For i = 9 To LastRow
If Cells(i,5).Value <> 0 And Cells(i,3).Value <> 0 Then Cells(i, 4).Value = Cells(i, 5).Value / Cells(i, 3).Value
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,716
Members
449,093
Latest member
Mnur

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