Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overflow Error

Status
Not open for further replies.

khansen97

Programmer
Oct 14, 2003
60
US
I am getting an overflow error everytime I try to use a variable. This is the code I have:

Dim xunits As Long
Dim xrevenue As Long
Dim xholdback As Long
Dim lcleaninvoice As Long
Dim lfinholdback As Long

ActiveSheet.Cells(gdpcarline, 8).Value = xunits ActiveSheet.Cells(gdpcarline, 9).Value = xrevenue

This is where I am getting the overflow error:

lcleaninvoice = xrevenue / xunits

Any help?
 
You are using a LONG data type - this stands for long INTEGER. Likelihood is that your division is producing non integer results - try using single or double - depending on how accurate you need to be

Rgds, Geoff

Never test the depth of water with both feet

Help us to help you by reading FAQ222-2244 before you ask a question
 
I found that it is coming up with an undefined result. Can you help me figure out a way to get around this? Can I put in an if statement?

thanks
 
As I said before - 1st step - try changing your data types

Rgds, Geoff

Never test the depth of water with both feet

Help us to help you by reading FAQ222-2244 before you ask a question
 
I did. And I am still getting the overflow error. This is only happening when my variable returns an undefined result.
 
What are the values in xrevenue and xunits?

From the code snippet provided, it looks like they are both zero. When they are both zero, you will get the Overflow error message every time.

perhaps you meant to say
Code:
    xunits = ActiveSheet.Cells(gdpcarline, 8).Value
  xrevenue = ActiveSheet.Cells(gdpcarline, 9).Value
instead of the other way around.
 
What is coming up with an "undefined result" - the variable type or the result of the formula ??
If the latter, what are the values of the xrevenue and xunits variables ??

Rgds, Geoff

Never test the depth of water with both feet

Help us to help you by reading FAQ222-2244 before you ask a question
 
xrevenue and xunits are both zero. I put in an if statement to filter out when they are both zero. This solved my problem. I was just trying to figure out if there was another way around this.
 
well you are ALWAYS gonna get an error if your divisor is 0 - why would you want anything else rather than an IF statement ???

Rgds, Geoff

Never test the depth of water with both feet

Help us to help you by reading FAQ222-2244 before you ask a question
 
You can use error trapping:

Replace the line:
Code:
   lcleaninvoice = xrevenue / xunits

with a function call like this:
Code:
   lcleaninvoice = CleanInvoice(xrevenue, xunits)

And code a function like this:
Code:
Function CleanInvoice(Revenue As Long, Units As Long) As Long
  On Error GoTo Skip
  CleanInvoice = Revenue / Units
  Exit Function
Skip:
  CleanInvoice = 0
End Function
But it is preferable to avoid the error as Geoff has suggested. Something like this:
Code:
If xunits = 0 Then
  lcleaninvoice = 0
Else
  lcleaninvoice = xrevenue / xunits
End If

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top