How to fix overflow error message in for next loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deekay
    New Member
    • May 2010
    • 21

    How to fix overflow error message in for next loop?

    I am using a straight forward For Next Loop which operates perfectly when I write
    Code:
    For n = 1 to numOfDigits
    'do something
    Next
    However when I change this to
    Code:
    For n = numOfDigits to 1 Step -1
    'do the same thing as before
    NEXT
    (where numOfDigits = 8 at this point in the code)
    I am getting an error message saying 'overflow' at the For... line of execution.

    Anybody know why?? Help gratefully received.

    Deekay
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi. There is nothing obvious from what you have posted. Could you post the entire code for the sub or function up to the NEXT itself? It is possibly related to the data types you have defined for the variables concerned, but I just can't tell without seeing the actual code.

    -Stewart

    Comment

    • TheSmileyCoder
      Recognized Expert Moderator Top Contributor
      • Dec 2009
      • 2322

      #3
      I tried this:
      Code:
      Public Sub Loopie()
      
      Dim intNumD As Integer
      intNumD = 8
      Dim I As Integer
      For I = 1 To intNumD
          Debug.Print I
      Next
      
      For I = intNumD To 1 Step -1
          Debug.Print I
      Next
      End Sub
      and it worked fine. Have you declared numOfDigits?

      Comment

      • Deekay
        New Member
        • May 2010
        • 21

        #4
        Hello Stewart
        Thanks for the reply. Here is the code
        Code:
        Private Function myFunc(numInput as Double) as Double
        Dim fstring as String
        Dim numOfDigits as Byte, counter as Byte
        dim char(8) as String
        
        fstring = Str(numInput)
        
        For counter = numOfDigits to 1 Step -1
        char(counter) = Mid(fstring, counter, 1)
        Next
        
        'some more code
        End Function
        Thanks for any thoughts.
        Deekay

        Comment

        • TheSmileyCoder
          Recognized Expert Moderator Top Contributor
          • Dec 2009
          • 2322

          #5
          If you change from byte to integer, your set to go.

          A byte cannot take on a negative value, as Im guessing thats why its complaining about the negative steps.

          Comment

          • Deekay
            New Member
            • May 2010
            • 21

            #6
            Great - it's obvious now you've said this!!! But I was tearing my hair out becuase this part of my code seemed so straight forward.
            Thanks a million
            Deekay

            Comment

            Working...