2013-02-23 91 views
0
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 

完成if語句後,行爲真的很奇怪。Excel VBA返回行爲奇怪,跳回

我跑它的調試器,這是它一跳:

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    ' 

所以基本上它要返回correct值,但隨後跳回到End if,並得到來自出藍色correct-1值。

+3

它是遞歸的事實不是問題嗎? – psubsee2003 2013-02-23 09:55:31

回答

6

你已經寫了一個遞歸函數!如果您使用調用堆棧,您將看到調試器將轉到您的函數的另一個實例。問題在於:

GetLastSameRow = GetLastSameRow(行+ 1,初值)

其被調用的函數的另一個實例。

+0

當然,謝謝 – Jaanus 2013-02-23 10:08:06

+2

一旦我看到它,就不相信自己足以知道這是正確的答案 – psubsee2003 2013-02-23 10:22:21