Excel VBA返回表现奇怪,跳回到

Jaa*_*nus 0 debugging excel vba excel-vba

Private Function GetLastSameRow(ByVal row, initialValue) As Integer
   .
   .
   .
   If (nextCell.Value = initialValue) Then
      GetLastSameRow = GetLastSameRow(row + 1, initialValue)
   End If
   MsgBox ("returning  : " & row)
   GetLastSameRow = row
End Function
Run Code Online (Sandbox Code Playgroud)

完成if语句后,行为真的很奇怪.

我在它上运行调试器,这就是它跳转的方式:

1. End If                            '
2. MsgBox ("returning  : " & row)    ' row value is 3
3. GetLastSameRow = row              '
4. MsgBox ("returning  : " & row)    ' row value is 2 ????????
5. GetLastSameRow = row              '
Run Code Online (Sandbox Code Playgroud)

所以基本上它想要返回correct值,但然后跳回到End if并从中获取correct-1价值.

And*_*own 6

你写了一个递归函数!如果使用调用堆栈,您将看到调试器将转到函数的不同实例.问题在于:

GetLastSameRow = GetLastSameRow(row + 1,initialValue)

这是调用该函数的另一个实例.

  • 我不相信自己,只要看到它就知道这是正确的答案 (2认同)