退出功能不起作用 - VBA

时间:2017-09-16 15:11:46

标签: vba excel-vba excel

如果值等于我正在寻找的值,或者行等于我正在寻找的行,我试图退出该函数。

但每次我使用Exit Function时,它都不起作用。如果我用End Function替换它,它告诉我我的If语句没有End。而我迷路了。

Function recursion(whereItEnds As Integer, lookingFor As Variant, currentMarker As Range, I As Integer, wsEverything As Worksheet) As Integer
    Dim col As Integer
    Dim newMarker As String
    newMarker = currentMarker.Value
    Dim currentMarker1 As Range
    recursion = 2
    col = 2
    If (StrComp(lookingFor, newMarker, vbTextCompare) = 0) Then
        Exit Function
    End If

    While (IsEmpty(wsEverything.Cells(col, "B").Value) = False)
        If (StrComp(wsEverything.Cells(col, "B").Value, newMarker, vbTextCompare) = 0) Then
            wsEverything.Cells.Range("A" & col, "F" & col).Copy
            Worksheets("Review").Cells.Range("A" & I).PasteSpecial
            Worksheets("Review").Cells.Range("G" & I).Value = col
            I = I + 1
            Set currentMarker1 = wsEverything.Cells(col, "E")

            If (col = whereItEnds) Then
                Exit Function
            End If
            recursion = recursion(whereItEnds, lookingFor, currentMarker1, I, wsEverything)
        End If
        col = col + 1
    Wend
End Function

我几乎完全没有想法为什么都不起作用。

编辑:它命中if语句,它进入那些代码。但是在调试时,它会触及“退出功能”,但它会继续运行。我只想让它结束声明。这是从另一张长纸上拉出数据并将其放在第二张纸上。它正在检查子父循环错误。未来的父母依赖于最初依赖它的孩子。

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

之前(Sheet1):

Sheet1

之后(评论表):

ReviewSheet

Option Explicit

Public Sub TestRecursion()

    Dim result As Variant, ws As Worksheet

    Set ws = Sheet1

    result = Recursion(ws.Cells(2, 8), ws.Cells(2, 2), ws.Cells(2, 5), 2, ws)

End Sub
Public Function Recursion(ByVal whereItEnds As Long, lookingFor As Variant, _
                          ByRef currentMarker As Range, ByVal i As Long, _
                          ByRef wsEverything As Worksheet) As Long

    Dim col As Long, newMarker As String, currentMarker1 As Range

    newMarker = currentMarker.Value
    Recursion = 2
    col = 2

    If StrComp(lookingFor, newMarker, vbTextCompare) = 0 Then Exit Function

    While Len(wsEverything.Cells(col, "B").Value2) > 0

        If StrComp(wsEverything.Cells(col, "B").Value2, newMarker, vbTextCompare) = 0 Then

            wsEverything.Cells.Range("A" & col, "F" & col).Copy
            Worksheets("Review").Cells.Range("A" & i).PasteSpecial
            Worksheets("Review").Cells.Range("G" & i).Value = col
            i = i + 1

            Set currentMarker1 = wsEverything.Cells(col, "E")

            If col = whereItEnds Then Exit Function

            Recursion = Recursion(whereItEnds, lookingFor, currentMarker1, i, wsEverything)
        End If
        col = col + 1
    Wend
End Function

如果是这样,您可以提供更多上下文来解释预期结果的逻辑

对于这个

,可能找到一个不那么复杂的解决方案