使用For Loop / VBA结束If / Next C语句

时间:2017-08-28 12:22:10

标签: excel vba excel-vba for-loop

我的For循环内部有一个相当复杂的循环,里面有一个If语句,另外一个For循环。并且在第二个For循环中给出了一定的标准(即If InStr(1, q.Value, "Total"))我想结束整个If语句并转移到Next C

据我所知,它像洋葱一样分层,可能没有简单的方法。

For Each C In copyRng

        If IsNumeric(C) And C.Value <> "0" And Len(C) <> 0 And C.Value <> "2017" Then 

            Set rowRange = xSheet.Range(C, C.EntireColumn.Cells(1)) 'set range from cell up to the top cell of the comment/ Fix the 2017 thing

            For Each q In rowRange 'Loop through that range and find the Account number just above it and set it as rowSrc
                If InStr(1, q.Value, "C-") And Not ISIN(C, uniqueVal) Then Set rowSrc = q
                If InStr(1, q.Value, "Total") Then End If 'At this point I want to leave the entire If Statement and move on to the next C
            Next q


            Set colSrc = C.EntireRow.Offset(0).Cells(1) 'find alert connected with the number
            numCol = DestSh.Cells.Find(colSrc.Value, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column 'look for the column in which the same alert is listed
            numRow = DestSh.Cells.Find(rowSrc.Value, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row 'look for row in which the same account is listed

            'Set destination
            Set destRng = DestSh.Cells(numRow, numCol)

            'Copy to destination Range
            C.Copy destRng

        End If

    Next C

2 个答案:

答案 0 :(得分:1)

您需要Java,然后在循环之后,将剩余的代码放在另一个Locale.ROOT块中:

Exit For

答案 1 :(得分:0)

删除Then后的每个下划线并重写整个代码。逻辑改变了。你差不多按照你的想法去其他地方。

检查一下: VBA - How the colon `:` works in VBA code with condition

通常,您的代码不会像您认为的那样工作。请检查以下内容:

Public Sub TestMe()

    If 1 = 2 Then _
        Debug.Print "I am true - 1=2"
        Debug.Print "I should be also true"

    If 2 = 3 Then _
        Debug.Print "I am true 2=3"

End Sub