+ Reply to Thread
Results 1 to 13 of 13

Error 6 - Overflow. What now?

Hybrid View

  1. #1
    Registered User
    Join Date
    09-04-2020
    Location
    Oslo
    MS-Off Ver
    Mac 2016
    Posts
    5

    Error 6 - Overflow. What now?

    I'm completely new to VBA and don't know very little programming in general.

    I am trying to run this VBA:

    hxxps : //github.com/niko86/RamerDouglasPeucker-ExcelVBA

    (not allowed to post live links)

    However I am getting an Error 6 - Overflow, and if I press Debug it points to this line:
    rowCount = UBound(pointList)
    Unfortunately I have no idea how to proceed from here. I've looked at the MS docs for both Error 6 and for the UBound function, but it's not been very fruitful.

    Any help would be appreciated.



    -----------
    Why am I trying to run this VBA? Well, I have a dataset with about 12000 datapoints. I need to graph this. But my computer is not really powerful enough to do so smoothly. So I was hoping to reduce the number of data points to about 1000. I don't need the graph to be accurate. It just needs to "look" similar enough. I'm trying to graph in GraphPad Prism. Not excel btw.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Error 6 - Overflow. What now?

    Hi there,

    It's VERY difficult to make meaningful suggestions without seeing the full code (which I can't access using the link you posted).

    The "Overflow" error means that a variable is being asked to store a value which exceeds the allowable limits for the type of variable.

    If your rowCount variable is declared (in a Dim statement) as being of type Integer, it might be worth declaring it to be of type Long and seeing if that makes a difference.

    Hope this helps.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    09-04-2020
    Location
    Oslo
    MS-Off Ver
    Mac 2016
    Posts
    5

    Re: Error 6 - Overflow. What now?

    Quote Originally Posted by Greg M View Post
    Hi there,

    It's VERY difficult to make meaningful suggestions without seeing the full code (which I can't access using the link you posted).

    The "Overflow" error means that a variable is being asked to store a value which exceeds the allowable limits for the type of variable.

    If your rowCount variable is declared (in a Dim statement) as being of type Integer, it might be worth declaring it to be of type Long and seeing if that makes a difference.

    Hope this helps.

    Regards,

    Greg M

    Here's the whole code:
    Sub CallDouglasPeucker()
    
    Dim pointList As Variant
    Dim rowCount As Integer
    Dim result As Variant
    Dim epsilon As Double
    
    Call ClearOutput
        
        pointList = Sheets("Sheet1").Range("Table1")
        
        rowCount = UBound(pointList)
        
        epsilon = Sheets("Sheet1").Range("epsilon")
        
        result = DouglasPeucker(pointList, epsilon, rowCount)
        
        WriteResult (result)
    
    End Sub
    
    Function DouglasPeucker(pointList As Variant, epsilon As Double, rowCount As Integer) As Variant
    
        Dim dMax As Double
        Dim Index As Integer
        Dim d As Double
        Dim arrResults1 As Variant
        Dim arrResults2 As Variant
        Dim resultList As Variant
        Dim recResults1 As Variant
        Dim recResults2 As Variant
        
        ' Find the point with the maximum distance
        dMax = 0
        Index = 0
        
        For i = 2 To (rowCount)
            d = Abs((pointList(rowCount, 1) - pointList(1, 1)) * (pointList(1, 2) - pointList(i, 2)) - (pointList(1, 1) - pointList(i, 1)) * (pointList(rowCount, 2) - pointList(1, 2))) / Sqr((pointList(rowCount, 1) - pointList(1, 1)) ^ 2 + (pointList(rowCount, 2) - pointList(1, 2)) ^ 2)
            If d > dMax Then
                Index = i
                dMax = d
            End If
        Next i
        
        'Testing if can stop cut going to index 0
        If Index > 1 Then
            arrResults1 = Cut_Array(pointList, 1, Index)
            arrResults2 = Cut_Array(pointList, Index, rowCount)
            
            ' If max distance is greater than epsilon, recursively simplify
            If (dMax > epsilon) Then
                ' Recursive call
                recResults1 = DouglasPeucker(arrResults1, epsilon, UBound(arrResults1))
                recResults2 = DouglasPeucker(arrResults2, epsilon, UBound(arrResults2))
        
                ' Build the result list
                resultList = Join_Array(recResults1, recResults2)
            Else
                ReDim resultList(1 To 2, 1 To 2)
                
                resultList(1, 1) = pointList(1, 1)
                resultList(1, 2) = pointList(1, 2)
                resultList(2, 1) = pointList(rowCount, 1)
                resultList(2, 2) = pointList(rowCount, 2)
            End If
        Else
            resultList = pointList
        End If
    
        ' Return the result
        DouglasPeucker = resultList
    
    End Function
    
    Function Cut_Array(arr As Variant, arrStart As Integer, arrEnd As Integer) As Variant
        
        Dim resultList As Variant
        
        ReDim resultList(1 To (arrEnd - arrStart) + 1, 1 To 2)
        
        For i = arrStart To arrEnd
            For j = 1 To 2
                resultList((i - arrStart) + 1, j) = arr(i, j)
            Next j
        Next i
        
        Cut_Array = resultList
            
    End Function
    
    Function Join_Array(arr1 As Variant, arr2 As Variant) As Variant
    
        Dim resultList As Variant
        Dim arr1Length As Integer
        Dim arr2Length As Integer
        
        arr1Length = UBound(arr1) - 1
        arr2Length = UBound(arr2)
        
        newArrLength = arr1Length + arr2Length
        
        ReDim resultList(1 To newArrLength, 1 To 2)
        
        For i = 1 To arr1Length
            For j = 1 To 2
              resultList(i, j) = arr1(i, j)
            Next j
        Next i
        
        For i = 1 To arr2Length
            For j = 1 To 2
              resultList(i + arr1Length, j) = arr2(i, j)
            Next j
        Next i
        
        Join_Array = resultList
        
    End Function
    
    Sub WriteResult(result As Variant)
            
        Dim Table2 As ListObject
        
        Set Table2 = Sheets("Sheet2").ListObjects("Table2")
    
        'Copy information loop
        For i = 1 To UBound(result)
            For j = 1 To UBound(result, 2)
                Table2.Range.Cells(i + 1, j).Value = result(i, j)
            Next j
        Next i
        
    End Sub
    
    Sub ClearOutput()
    
        Dim OutputTable As ListObject
        Dim StartRow As Integer
        
        Set OutputTable = Sheets("Sheet2").ListObjects("Table2")
        
        StartRow = OutputTable.Range.Cells(1, 1).Row + 1
        If Not OutputTable.InsertRowRange Is Nothing Then
            'Pass
        Else
            Sheets("Sheet2").Rows(StartRow & ":" & (OutputTable.DataBodyRange.Rows.Count + StartRow)).Delete
        End If
        
    End Sub
    I tried changing:

    Dim rowCount As Integer 
    to 
    Dim rowCount As Long.
    That produced this error:
    Compile error:
    
    ByRef argument type mismatch
    After that the debugger points to "result = DouglasPeucker(pointList, epsilon, rowCount)"
    Last edited by jolink; 09-04-2020 at 10:10 AM.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Error 6 - Overflow. What now?

    Two more changes required:

    Function DouglasPeucker(pointList As Variant, epsilon As Double, rowCount As Long) As Variant
    
        Dim dMax As Double
        Dim Index As Long
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Registered User
    Join Date
    09-04-2020
    Location
    Oslo
    MS-Off Ver
    Mac 2016
    Posts
    5

    Re: Error 6 - Overflow. What now?

    Awesome! Thank you both That cleared that problem. But now I have a new error.

    Error 13 - Type Mismatch. It points to this line.
    epsilon = Sheets("Sheet1").Range("epsilon")
    In the instructions to use this VBA it says you need to define a range in your excel book and name it epsilon. I'm not entirely sure I've done that the correct way. Maybe that is what is giving me this error? How does VBA want you to define a range anyway?

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Error 6 - Overflow. What now?

    Hi again,

    Thanks for posting your code.

    The "Type Mismatch" error is generated when a variable of type (e.g.) Long is passed to a routine in which the corresponding dummy argument is declared to be of type (e.g.) Integer.

    I've changed various declarations in your code to achieve consistency. Please note that I haven't attempted to understand what your code does, I've just got it to compile successfully.

    The modified code is as follows:

    
    
    
    Option Explicit
    
    
    Sub CallDouglasPeucker()
    
    Dim pointList As Variant
    Dim rowCount As Long
    Dim result As Variant
    Dim epsilon As Double
    
    Call ClearOutput
        
        pointList = Sheets("Sheet1").Range("Table1")
        
        rowCount = UBound(pointList)
        
        epsilon = Sheets("Sheet1").Range("epsilon")
        
        result = DouglasPeucker(pointList, epsilon, rowCount)
        
        WriteResult (result)
    
    End Sub
    
    
    Function DouglasPeucker(pointList As Variant, epsilon As Double, rowCount As Long) As Variant
    
        Dim dMax As Double
        Dim Index As Long
        Dim d As Double
        Dim arrResults1 As Variant
        Dim arrResults2 As Variant
        Dim resultList As Variant
        Dim recResults1 As Variant
        Dim recResults2 As Variant
        Dim i As Long
        
        ' Find the point with the maximum distance
        dMax = 0
        Index = 0
        
        For i = 2 To (rowCount)
            d = Abs((pointList(rowCount, 1) - pointList(1, 1)) * (pointList(1, 2) - pointList(i, 2)) - (pointList(1, 1) - pointList(i, 1)) * (pointList(rowCount, 2) - pointList(1, 2))) / Sqr((pointList(rowCount, 1) - pointList(1, 1)) ^ 2 + (pointList(rowCount, 2) - pointList(1, 2)) ^ 2)
            If d > dMax Then
                Index = i
                dMax = d
            End If
        Next i
        
        'Testing if can stop cut going to index 0
        If Index > 1 Then
            arrResults1 = Cut_Array(pointList, 1, Index)
            arrResults2 = Cut_Array(pointList, Index, rowCount)
            
            ' If max distance is greater than epsilon, recursively simplify
            If (dMax > epsilon) Then
                ' Recursive call
                recResults1 = DouglasPeucker(arrResults1, epsilon, UBound(arrResults1))
                recResults2 = DouglasPeucker(arrResults2, epsilon, UBound(arrResults2))
        
                ' Build the result list
                resultList = Join_Array(recResults1, recResults2)
            Else
                ReDim resultList(1 To 2, 1 To 2)
                
                resultList(1, 1) = pointList(1, 1)
                resultList(1, 2) = pointList(1, 2)
                resultList(2, 1) = pointList(rowCount, 1)
                resultList(2, 2) = pointList(rowCount, 2)
            End If
        Else
            resultList = pointList
        End If
    
        ' Return the result
        DouglasPeucker = resultList
    
    End Function
    
    
    Function Cut_Array(arr As Variant, arrStart As Long, arrEnd As Long) As Variant
        
        Dim resultList As Variant
        Dim i As Long
        Dim j As Long
    
        ReDim resultList(1 To (arrEnd - arrStart) + 1, 1 To 2)
        
        For i = arrStart To arrEnd
            For j = 1 To 2
                resultList((i - arrStart) + 1, j) = arr(i, j)
            Next j
        Next i
        
        Cut_Array = resultList
            
    End Function
    
    
    Function Join_Array(arr1 As Variant, arr2 As Variant) As Variant
    
        Dim resultList As Variant
        Dim arr1Length As Integer
        Dim arr2Length As Integer
        Dim newArrLength As Long
        Dim i As Long
        Dim j As Long
    
        arr1Length = UBound(arr1) - 1
        arr2Length = UBound(arr2)
        
        newArrLength = arr1Length + arr2Length
        
        ReDim resultList(1 To newArrLength, 1 To 2)
        
        For i = 1 To arr1Length
            For j = 1 To 2
              resultList(i, j) = arr1(i, j)
            Next j
        Next i
        
        For i = 1 To arr2Length
            For j = 1 To 2
              resultList(i + arr1Length, j) = arr2(i, j)
            Next j
        Next i
        
        Join_Array = resultList
        
    End Function
    
    
    Sub WriteResult(result As Variant)
            
        Dim Table2 As ListObject
        Dim i As Long
        Dim j As Long
    
        Set Table2 = Sheets("Sheet2").ListObjects("Table2")
    
        'Copy information loop
        For i = 1 To UBound(result)
            For j = 1 To UBound(result, 2)
                Table2.Range.Cells(i + 1, j).Value = result(i, j)
            Next j
        Next i
        
    End Sub
    
    
    Sub ClearOutput()
    
        Dim OutputTable As ListObject
        Dim StartRow As Integer
        
        Set OutputTable = Sheets("Sheet2").ListObjects("Table2")
        
        StartRow = OutputTable.Range.Cells(1, 1).Row + 1
        If Not OutputTable.InsertRowRange Is Nothing Then
            'Pass
        Else
            Sheets("Sheet2").Rows(StartRow & ":" & (OutputTable.DataBodyRange.Rows.Count + StartRow)).Delete
        End If
        
    End Sub

    Hope this helps.

    Regards,

    Greg M

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Error 6 - Overflow. What now?

    It needs to be a one cell range with a number in it - is it?

  8. #8
    Registered User
    Join Date
    09-04-2020
    Location
    Oslo
    MS-Off Ver
    Mac 2016
    Posts
    5

    Re: Error 6 - Overflow. What now?

    Quote Originally Posted by rorya View Post
    It needs to be a one cell range with a number in it - is it?
    Currently it is not. I am working from the assumption that epsilon is the target number for the macro, I want to reduce my data to 1000, so I tried creating a range that spans a 1000 cells. Probably not the way to do it eh?

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,522

    Re: Error 6 - Overflow. What now?

    epsilon is dimmed as double (a single value -- not an array). I am guessing the the range named "epsilon" consists of multiple cells. A double cannot hold a range nor an array of doubles. Try dimming epsilon as variant (it will become a variant containing an array).
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  10. #10
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Error 6 - Overflow. What now?

    Hi again,

    You don't need to define a named range using VBA - you can do it, but it's probably a bit easier to do it in "native" Excel.

    On the Ribbon, click on FORMULAS >> Name Manager, and specify the name ("epsilon") of the range, and the range of cells to which you want that name to be applied.

    Hope this helps.

    Regards,

    Greg M

  11. #11
    Registered User
    Join Date
    09-04-2020
    Location
    Oslo
    MS-Off Ver
    Mac 2016
    Posts
    5

    Re: Error 6 - Overflow. What now?

    Quote Originally Posted by Greg M View Post
    Hi again,

    You don't need to define a named range using VBA - you can do it, but it's probably a bit easier to do it in "native" Excel.

    On the Ribbon, click on FORMULAS >> Name Manager, and specify the name ("epsilon") of the range, and the range of cells to which you want that name to be applied.

    Hope this helps.

    Regards,

    Greg M
    That solved it. Now it runs as intended. Thank you so much!!

  12. #12
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Error 6 - Overflow. What now?

    If "epsilon" is declared as a Range, the "Type Mismatch" error you indicated can be avoided by using:

    
    Set epsilon = Sheets("Sheet1").Range("epsilon")
    Regards,

    Greg M

  13. #13
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,645

    Re: Error 6 - Overflow. What now?

    Hi again,

    Many thanks for all of your feedback.

    You're welcome - glad I was able to help.

    Regards,

    Greg M

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Ribbon Customization error (Long Data Type but still overflow error)
    By ImranBhatti in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-30-2018, 08:15 AM
  2. [SOLVED] OVerflow 6 error
    By Mortada in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-25-2013, 06:09 PM
  3. Explanation of the Run-time error '6': Overflow Error
    By mgphill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-25-2012, 10:46 AM
  4. Want to do a while-loop but get error message: run error '6' overflow
    By danzi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-07-2011, 01:48 PM
  5. Overflow Error
    By transparencia in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-25-2010, 02:21 PM
  6. Overflow Error
    By narrowgate88 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-10-2010, 02:08 AM
  7. [SOLVED] Overflow error
    By Jim Berglund in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-30-2005, 02:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1