简体   繁体   中英

Out of stack space when using recursion in function

My function looks like this:

Sub sortNumbers()
Dim i As Integer
Dim j As Integer
Dim highestNumber As Integer
For i = 1 To 8
    If IsEmpty(Cells(i + 4, 6).Value) = False Then
        If Cells(i + 3, 6).Value > Cells(i + 4, 6).Value Then
            highestNumber = Cells(i + 3, 6).Value
            Cells(i + 3, 6).Value = Cells(i + 4, 6).Value
            Cells(i + 4, 6).Value = highestNumber
        End If
    End If
Next i

For j = 1 To 8
    If IsEmpty(Cells(j + 4, 6).Value) = False Then
        If Cells(i + 3, 6).Value > Cells(i + 4, 6).Value Then
            Call sortNumbers
        Else
            Exit Sub
        End If
    End If
Next j

End Sub

Everything gets sorted properly, but right after I get a message saying Out of stack space

Any help would be much appreciated!

EDIT

The excel sample data looks like this:

test data

1

100

1000

8

9

9

50

100

500

(from F3-F12)

If you remove the IsEmpty lines, Empty cells will be treated as 0. If you wish to leave them blank and sort around them you will need to impliment additional logic.

Your second loop needed to be adjusted. As it stood, the first time

call 1: The first loop would give: 1 100 8 9 9 50 100 500 1000

Then the second loop would get to 1 > 100 and exit sub.

BUT... its best not to simply remove the exit sub call. Its more efficient to only recall sortNumbers once per call.

If you had simply removed the exit sub. then the second loop would get to 100 > 8 and trigger a recursion (Call 2).

Call 2: the first loop would give: 1 8 9 9 50 100 100 500 1000

then the second loop would determine that Cells(i + 3,6) is never > Cells(i + 4,6) and exit Sub.

Since the 2nd call has returned we resume Call 1 where we left off. This means we finish the 2nd loop.

If this were a larger dataset you could have hundreds of recursions required to sort the dataset. When the last call (lets say its the 104th call) returns the previous 103 calls to the routine would all finish their 2nd loops (which since the 104th call returned, the data is already sorted, and thus is a waste)

The second loop should simply check to see if a recall is nessisary and if so, recall sortNumbers one time.

Sub sortNumbers()
Dim i As Integer
Dim j As Integer
Dim highestNumber As Integer
For i = 1 To 8
    If Cells(i + 3, 6).value > Cells(i + 4, 6).value Then
        highestNumber = Cells(i + 3, 6).value
        Cells(i + 3, 6).value = Cells(i + 4, 6).value
        Cells(i + 4, 6).value = highestNumber
    End If
Next i

Dim ReCall As Boolean
ReCall = False
For i = 1 To 8
    If Cells(i + 3, 6).value > Cells(i + 4, 6).value Then
        ReCall = True
        i = 8
    End If
Next i
If ReCall Then Call sortNumbers

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM