OVERFLOW

Mark_Elford

Board Regular
Joined
Feb 5, 2003
Messages
97
HI, I am having to debug a xls macro, but I keep coming across a OVERFLOW..

What does this mean and why do they occour, Help, doesnt help, and it doesnt let me debug from the point it overflowd.


The macro, opens other worksheets scans down and matches agent with agents on the master sheet. Then copies the relevant data. It does this lots of times for data in several sheets.

The code looks something like this.

Code:
Option Explicit

Public Const FilePath = "F:\DATA\FULFIL\Operations Support\Agent Performance\New Agent Performance"
Public Const FilePath2 = "F:\DATA\FULFIL\Operations Support\Agent Performance\New Agent Performance\IRT"
Public Const NO_OF_BLANK_LINES_ALLOWED As Integer = 3

Public IRT As String
Public RawData As Range, NewData As Range
Public iRow As Integer, iRow2 As Integer, BlankLineCount As Integer, NoMatch As Integer, bca As Integer
Public fs As Object



Public wcDate As Date, conDate As Date
Public midDate As String 'Not actually a date variable but to hold the value of conDate stripped of "/"
Public DayDate 'converts wcDate to an catula day i.e. Monday
Public Agent

Public WorkingFile As String

Public blnOpen As Boolean


Public Function IsOpen(WorkBookName As String) As Boolean
'Returns True if WorkBookName is open
'otherwise false
On Error GoTo NotOpen

Dim strName As String
  
  strName = Workbooks(WorkBookName).Name
  IsOpen = True
    Exit Function

NotOpen:
  IsOpen = False

End Function
Public Sub AddZero()

Dim c As Range

Range("a1").Select

For Each c In ActiveCell.CurrentRegion.Cells
  If Mid(c, 1, 1) = ":" Then
    c = 0 & c
  End If
Next

End Sub

Public Sub CSAgentsTop_Macro()
On Error GoTo Trap

Application.ScreenUpdating = False
Application.DisplayAlerts = False

'Check users are running this code with the right workbook open.
blnOpen = IsOpen("CSAgentsTop.xls")

If blnOpen = False Then
  MsgBox "Make sure you are in CSAgentsTop.xls workbook" _
  & vbNewLine & "before running the macro AllCSAgents"
  Exit Sub
End If

Call AddZero

'Now that we are in the right workbook do the stuff
wcDate = Range("B1").Value
'First find the week commencing date
conDate = wcDate + (1 - WorksheetFunction.Weekday(wcDate, 2))

midDate = Mid(conDate, 1, 2)
midDate = midDate & Mid(conDate, 4, 2)
midDate = midDate & Mid(conDate, 7, 2)
DayDate = Format(wcDate, "dddd")

Set RawData = ActiveSheet.Cells
iRow = 5
Do
iRow = iRow + 1
If RawData(iRow, 1) = "" Then
  BlankLineCount = BlankLineCount + 1
Else
  BlankLineCount = 0
  Agent = RawData(iRow, 1)
  
  Set fs = Application.FileSearch
  With fs
    .LookIn = FilePath
    .Filename = midDate & ".xls"
    .SearchSubFolders = True
    If .Execute > 0 Then
    
      For bca = 1 To fs.FoundFiles.Count
      If InStr(fs.FoundFiles(bca), "IRT") Then
        
      Else
      
          If InStr(fs.FoundFiles(bca), midDate & ".xls") <> 0 Then
            Workbooks.Open fs.FoundFiles(bca)
            WorkingFile = ActiveWorkbook.Name
            ActiveWorkbook.Worksheets(DayDate).Activate
            IRT = ActiveSheet.Range("c3").Value
            Dim Lan As Integer
            
            
' **************************************************************************
            
            Lan = 38 ' Agents if agent count increases add more agents here
            
' **************************************************************************
            
            'If IRT = "IRT" Then  'Extra
            '  Lan = 50           '
            'Else                 '
            '  Lan = 38           '
            'End If               '
            Set NewData = ActiveSheet.Cells
            iRow2 = 5
            Do
            iRow2 = iRow2 + 1
              If Trim(NewData(iRow2, 1)) = Agent Then
                If IRT <> "IRT" Then
                  Call InsertValues1
                Else
                  Call InsertValues2
                End If
              Else
                NoMatch = NoMatch + 1
              End If
              Loop Until iRow2 = Lan 'NEED TO ALTER WHAT IT LOOPS UNTIL
                                    'DEPENDING ON THE REPORT OPEN
              Workbooks(WorkingFile).Close savechanges:=True 'closes the file opened
              End If
          End If
         
      Next bca
      '------------------------------
    End If
End With
End If
Loop Until iRow = 160
'Loop Until BlankLineCount = NO_OF_BLANK_LINES_ALLOWED

Application.ScreenUpdating = True
Application.DisplayAlerts = True

Exit_Trap:
  Exit Sub

Trap:
  MsgBox Err.Description
  Resume Exit_Trap
End Sub

Public Sub InsertValues1()
'These figures for non IRT Workbooks and top half
NewData(iRow2, 1).Offset(0, 2).Value = RawData(iRow, 1).Offset(0, 1).Value
NewData(iRow2, 1).Offset(0, 6).Value = RawData(iRow, 1).Offset(0, 8).Value
NewData(iRow2, 1).Offset(0, 8).Value = RawData(iRow, 1).Offset(0, 9).Value
NewData(iRow2, 1).Offset(0, 9).Value = RawData(iRow, 1).Offset(0, 11).Value
NewData(iRow2, 1).Offset(0, 5).Value = RawData(iRow, 1).Offset(0, 14).Value

End Sub

Public Sub InsertValues2()
'IRT Top figures. These are IRT Agents within the ALL CS Agents
NewData(iRow2, 1).Offset(0, 2).Value = RawData(iRow, 1).Offset(0, 1).Value
NewData(iRow2, 1).Offset(0, 5).Value = RawData(iRow, 1).Offset(0, 8).Value
NewData(iRow2, 1).Offset(0, 8).Value = RawData(iRow, 1).Offset(0, 9).Value
NewData(iRow2, 1).Offset(0, 9).Value = RawData(iRow, 1).Offset(0, 13).Value
NewData(iRow2, 1).Offset(0, 4).Value = RawData(iRow, 1).Offset(0, 14).Value

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Change all your variables dimmed as Integer to Long . e,g,

Public iRow As Integer

Change to:
Public iRow as Long

If you are using more than 32,768 rows, a variable defined as an Integer will produce the overflow message.

From the VBA help file:

Integer variables are stored as 16-bit (2-byte) numbers ranging in value from -32,768 to 32,767. Long (long integer) variables are stored as signed 32-bit (4-byte) numbers ranging in value from -2,147,483,648 to 2,147,483,647.

Hopefully, this may solve the problem.

Regards,

Mike
 
Upvote 0
In a 32-bit you're going to end up with a long integer anyways... So instead of getting your smaller integer, VBA ends up having to convert your 2-byte integer to the 4-byte in any case. You're better of using long over integer with current systems in any case. :)

Link.
 
Upvote 0
Ok, but I dont think my Irows ever exceed 150 rows anyway.


Could it be a memory thing? or does anything else trigger an overflow.


Excuse my nievity,? but would formating from onesheet to another cause this form of problem. In my code, I am copying a value from one sheet to another, if the formating doesnt match will this cause the same error?

Code:
NewData(iRow2, 1).Offset(0, 2).Value = RawData(iRow, 1).Offset(0, 1).Value
NewData(iRow2, 1).Offset(0, 6).Value = RawData(iRow, 1).Offset(0, 8).Value
NewData(iRow2, 1).Offset(0, 8).Value = RawData(iRow, 1).Offset(0, 9).Value
NewData(iRow2, 1).Offset(0, 9).Value = RawData(iRow, 1).Offset(0, 11).Value
NewData(iRow2, 1).Offset(0, 5).Value = RawData(iRow, 1).Offset(0, 14).Value

if so is there a way to force the destination cell with the same format as the cell copied from ?
 
Upvote 0
You have a lot more integer varibles than that, i.e.,

Public iRow As Integer, iRow2 As Integer, BlankLineCount As Integer, NoMatch As Integer, bca As Integer

Change all of them. What line of code inspires debugging?

Formatting should have pretty close to nothing to do with it.
 
Upvote 0
Ok, :oops:

So I have a few more, all of which shouldnt exceed that anyway..

Soo its not formatting? what about memory? as sometimes it results in a dr watson?

I dont know what line it bungs on, as it just says overflow and doesnt give a debug option.


Thanks for being patient with me...
 
Upvote 0
Mark_Elford said:
I dont know what line it bungs on, as it just says overflow and doesnt give a debug option.

Thanks for being patient with me...
{snip}

No problem. As initially advised, change all of these :oops: I've given you a 2nd reason to do this as well.

When I run the following:

Sub test()
Dim i As Integer
i = 33000
End Sub

I get run-time 6, Overflow. The same dialog has three options:

End, Debug, Help.

If I hit Debug, it takes me right to the naughty line. You're certain you don't see this dialog?
 
Upvote 0
Mark,

A quick Google search produced the following, some of which may be relevant to your problem.

An Overflow error has been attributed to:

* Insufficient memory

*Corrupted data

* Importing ASCII garbage or symbols

* Data unrecognizable to Excel

* More than 256 columns

* More than 65,656 rows

* Division by zero

* A text field in Access containing a string of characters that can be interpreted as a numeric value outside Excel's limits, e.g. "123456E7890". This would export correctly to a textfile but it's conceivable that in exporting to Excel it would be interpreted as a number (and trigger an overflow error) rather than a string.

See this macro:
Rich (BB code):
Sub Test()
    Dim Sale As Integer
    Dim Charge As Long
    Sale = 143
    If Sale > 100 Then Charge = _
        167500 + (Sale - 100) * 1000
End Sub
Seems simple enough but the macro produces an overflow error because the result is way over 32767 (for a integer).

VBA Help file:
An overflow results when you try to make an assignment that exceeds the limitations of the target of the assignment. This error has the following causes and solutions:
• The result of an assignment, calculation, or data type conversion is too large to be represented within the range of values allowed for that type of variable.
Assign the value to a variable of a type that can hold a larger range of values.
• An assignment to a property exceeds the maximum value the property can accept.
Make sure your assignment fits the range for the property to which it is made.
• You attempt to use a number in a calculation, and that number is coerced into an integer, but the result is larger than an integer. For example:
• Dim x As Long
x = 2000 * 365 ' Error: Overflow

To work around this situation, type the number, like this:
Dim x As Long
x = CLng(2000) * 365

Regards,


Mike
 
Upvote 0
Thanks, for the help everyone,,

I have finaly changed all instances of the integer to long, and at first this didnt seem to fix the problem. As the Dr Watson kept coming up..

However we have just identified that the Good Dr, only comes up when a email comes in or I get board of the macro running and go and surf the net, In short anything other than leaving the macro running on its own causes the watson to appear and the macro to crash...

After leaving well alone and interupted the macro seems to work ok for now...


Thanks again peeps

(y)
 
Upvote 0

Forum statistics

Threads
1,215,320
Messages
6,124,238
Members
449,149
Latest member
mwdbActuary

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