【问题标题】:End If/Next C Statement with For Loop / VBA使用 For 循环/VBA 结束 If/Next C 语句
【发布时间】:2017-08-28 20:58:59
【问题描述】:

我有一个相当复杂的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

【问题讨论】:

  • If 语句不能那样工作。您的If..Then End If 行实际上只是在说If...Then : Do Nothing : End If,因为End If 只是If 块上的最后一行。同样,您的行 If...Then _ 是有问题的,因为它们是行继续标记。不要那样使用它们。它使您的代码难以阅读和调试。继续下一个 C 的唯一方法是将您不想运行的代码包装在 if If Not Instr() Then 的反向中,然后,由于条件不满足,下一个 C 将运行。
  • @BrandonBarney 感谢您的帮助。我真的不确定 If 语句是如何工作的,现在它更有意义了。

标签: vba excel for-loop


【解决方案1】:

您需要Exit For,然后在循环之后,将剩余代码放入另一个If 块中:

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))

        For Each q In rowRange
            If InStr(1, q.Value, "C-") And Not ISIN(C, uniqueVal) Then Set rowSrc = q
            If InStr(1, q.Value, "Total") Then Exit For ' Exit loop prematurely
        Next q
        If q is Nothing Then ' Skip if loop was exited prematurely
            Set colSrc = C.EntireRow.Offset(0).Cells(1)
            numCol = DestSh.Cells.Find(colSrc.Value, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
            numRow = DestSh.Cells.Find(rowSrc.Value, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row

            Set destRng = DestSh.Cells(numRow, numCol)
            C.Copy destRng
        End If
    End If
Next C

【讨论】:

  • 感谢您的帮助。我意识到 If 语句的工作方式与我想象的不同。我决定尝试您的代码,但收到错误消息“未识别对象变量或 With-Variable”。是否有一个快速解决方案,或者这可能是我其余代码的一个更大的问题。 'Next q' 行是否应该下移?
  • 你在哪里定义 q 以及你在哪一行得到错误?注意:不,Next q 不应向下移动。
  • 我在这一行得到错误 'If InStr(1, q.Value, "Total") = 0 Then ' Skip if loop was early exit' 我在开头定义了 q代码为范围。
  • 好的,你能把它换成If q Is Nothing Then吗?
  • 我认为这会适得其反,因为我希望代码设置 destRng 并将 C 复制到其中 if 'InStr(1, q.Value, "C-") And Not ISIN(C, uniqueVal) ' 而不是如果 q 什么都不是。这有意义吗?
【解决方案2】:

去掉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

【讨论】:

  • 非常感谢。我需要停止如此随意地使用“Then _”以确保代码更清晰。
  • @FSchildorfer - 作为第二步,尝试将Option Explicit 放在代码的顶部。不应允许编译 If InStr(1, q.Value, "Total") Then End If 之类的东西。
  • 我一定会的。
猜你喜欢
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 2013-08-26
  • 2019-11-03
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 2018-05-20
相关资源
最近更新 更多