Results 1 to 7 of 7

Overflow Error 6 on Divide by 2

Threaded View

  1. #1
    Registered User
    Join Date
    01-24-2021
    Location
    Winchester, England
    MS-Off Ver
    Office 365 Excel for Mac v16.63
    Posts
    6

    Overflow Error 6 on Divide by 2

    I've been developing a macro that needs to put values into a one-dimensional array for filtering a table by month. In some places, my code works and in others, the same piece of code returns an Overflow Error. The problem occurs when I divide the loop counter by two. This division returns the Overflow error.
    I have distilled the problem code into a separate macro in a new workbook and I am still getting the error. The macro in its entirety is:

    Sub TestOverflow()
    
    Dim j As Integer
    Dim rngRows As Integer
    
    rngRows = 3
    
    Debug.Print "rngRows = " & rngRows
     For j = 1 To rngRows * 2
         If j Mod 2 = 0 Then
             Debug.Print "j is Even, j = " & j
             Debug.Print "j divided by 2 is " & j / 2
         Else
             Debug.Print "j is Odd, j = " & j
         End If
     Next j
            
    End Sub
    The error occurs at the line
    Debug.Print "j divided by 2 is " & j / 2

    For some reason, dividing the even integer j by 2 returns an overflow error.
    In this macro, the maximum value of j is 6, so this should not cause an integer variable to overflow. Only even numbers are divided by 2, so the result should always be an integer.

    Things I have already tried:
    1. Declaring j as types other than Integer: Long, Double,
    2. Changing the Debug.Print line to print Int(j / 2)
    3. Changing the Debug.Print line to print Int(j) / 2
    4. Leaving j as Integer and converting the value in j to a new variable, i, which is Long or Double


    Anyone any idea why this error is occurring and how to solve it?
    Attached Files Attached Files
    Last edited by johnv64; 01-24-2021 at 07:42 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Error 6 - Overflow. What now?
    By jolink in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-04-2020, 10:45 AM
  2. [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
  3. Overflow error, can't see why
    By Alexander_V in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-18-2012, 11:57 AM
  4. 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
  5. 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
  6. Overflow error, need help
    By mkerstei in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-13-2006, 10:20 PM
  7. [SOLVED] Overflow error
    By Jim Berglund in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-30-2005, 02:06 PM

Tags for this Thread

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