【问题标题】:For loop being skipped nested within an IF statement, using VBA使用 VBA 跳过嵌套在 IF 语句中的 For 循环
【发布时间】:2013-10-23 12:25:37
【问题描述】:

我有一个 VBA 宏,我已经研究了一段时间。我几乎把它放到了我想要的地方,但现在我跳过了一个 for 循环。我将其设置为检查一个数字,然后进入 for 循环并遍历一系列行以查找某些信息。使用断点我可以看到它检查了 IF 语句,然后点击了 For 语句,但随后它跳到了 End If 并且从不点击 for 循环内的内容。这是有问题的代码,而不是整个宏(如果需要整个东西,我可以添加它):

        If (BranchNumber) Like "0" Or (BranchNumber) Like "1" Or (BranchNumber) Like "2" Or (BranchNumber) Like "3" Or (BranchNumber) Like "4" Or (BranchNumber) Like "5" Then

        fRow = Sheets(2).Range("A188").End(xlUp).Row

        For f = fRow To 3 Step -1
        Range("A" & f).Select
            For CountDown = 0 To 30 Step 1
                If (Range("A" & f).Value) Like Tellers(CountDown) Then
                    TellerName(count) = Range("B" & f).Value
                    MsgBox (CountDown)
                    MsgBox (Tellers(CountDown))
                    MsgBox (count)
                    MsgBox (TellerName(count))
                    MsgBox (TellerTotal(count))
                    If (BranchNumber) Like "0" Then
                        TellerTotal(count) = Range("C" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                    If (BranchNumber) Like "1" Then
                        TellerTotal(count) = Range("D" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                    If (BranchNumber) Like "2" Then
                        TellerTotal(count) = Range("E" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                    If (BranchNumber) Like "3" Then
                        TellerTotal(count) = Range("F" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                    If (BranchNumber) Like "4" Then
                        TellerTotal(count) = Range("G" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                    If (BranchNumber) Like "5" Then
                        TellerTotal(count) = Range("H" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                End If
            Next
        Next f
    End If

这是第一个遇到for f = fRow To 3 Step -1 的For 循环,然后它跳到最后一个End If 并继续而不执行for 循环中的代码。 有什么想法吗?

好的,我正在添加宏的其余部分以尝试澄清一些问题:

Sub GetInfo()
Dim NumberBranches
Dim Branch
Dim Tellers(30), TellerTotal(30), TellerName(30)
Dim count As Long
Dim CountDown
Dim l, f As Long
Dim lRow, fRow As Long
Dim BranchNumber As Long
Dim Branches As Long
Dim lngIndex As Long
Dim strPath() As String

NumberBranches = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "11", "12", "13", "16", "18", "19", "20")

For Branches = 0 To 15 Step 1
    BranchNumber = InputBox("What is the branch number?", "Branch Number", NumberBranches(Branches))

    Branch = Application.GetOpenFilename("Excel files (*.xls), *.xls")
    'Application.ScreenUpdating = False
    Workbooks.Open FileName:=Branch
    strPath() = Split(path, "\")
    lngIndex = UBound(strPath)
    Branch = Mid(Mid(Branch, InStrRev(Branch, "/") + 1), InStrRev(Branch, "\") + 1)
    Windows(Branch).Activate

    lRow = Sheets(1).Range("A200").End(xlUp).Row
    MsgBox (lRow)
    count = 0
    For l = lRow To 1 Step -1
    MsgBox (l)
    MsgBox (lRow)
    Sheets(1).Activate
    Range("A" & l).Select
        If (BranchNumber) Like "19" Then
            If (Range("B" & l).Value) Like "TC:*" Then
                Tellers(count) = Replace(Range("B" & l).Value, "TC: ", "")
                count = count + 1
            End If
            If (Range("H" & l).Value) Like "TC:*" Then
                Tellers(count) = Replace(Range("H" & l).Value, "TC: ", "")
                count = count + 1
            End If
        End If
        If BranchNumber <> "19" Then
            If (Range("A" & l).Value) Like "TC:" Then
                Tellers(count) = Range("B" & l).Value
                count = count + 1
            End If
            If (Range("H" & l).Value) Like "TC:" Then
                Tellers(count) = Range("I" & l).Value
                count = count + 1
            End If
        End If
    Next l

    Windows("TellerTranCounts.xlsm").Activate
    Sheets(2).Activate

    If (BranchNumber) = "0" Or (BranchNumber) = "1" Or (BranchNumber) = "2" Or (BranchNumber) = "3" Or (BranchNumber) = "4" Or (BranchNumber) = "5" Then
        fRow = Sheets(2).Range("A188").End(xlUp).Row
        MsgBox (fRow)
        MsgBox (f)
        For f = fRow To 3 Step -1
        MsgBox (f)
        Range("A" & f).Select
            For CountDown = 0 To 30 Step 1
                If (Range("A" & f).Value) Like Tellers(CountDown) Then
                    TellerName(count) = Range("B" & f).Value
                    MsgBox (CountDown)
                    MsgBox (Tellers(CountDown))
                    MsgBox (count)
                    MsgBox (TellerName(count))
                    MsgBox (TellerTotal(count))
                    If (BranchNumber) Like "0" Then
                        TellerTotal(count) = Range("C" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                    If (BranchNumber) Like "1" Then
                        TellerTotal(count) = Range("D" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                    If (BranchNumber) Like "2" Then
                        TellerTotal(count) = Range("E" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                    If (BranchNumber) Like "3" Then
                        TellerTotal(count) = Range("F" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                    If (BranchNumber) Like "4" Then
                        TellerTotal(count) = Range("G" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                    If (BranchNumber) Like "5" Then
                        TellerTotal(count) = Range("H" & f).Value
                        MsgBox (TellerTotal(count))
                        count = count + 1
                    End If
                End If
            Next
        Next f
    End If
    If (BranchNumber) Like "6" Or (BranchNumber) Like "7" Or (BranchNumber) Like "8" Or (BranchNumber) Like "9" Or (BranchNumber) Like "11" Or (BranchNumber) Like "12" Then
        fRow = Sheets(2).Range("A375").End(xlUp).Row

        For f = fRow To 189 Step -1
        Range("A" & f).Select
            For CountDown = 0 To 30 Step 1
                If (Range("A" & f).Value) Like Tellers(CountDown) Then
                    TellerName(count) = Range("B" & f).Value
                    If (BranchNumber) Like "6" Then
                        TellerTotal(count) = Range("C" & f).Value
                        count = count + 1
                    End If
                    If (BranchNumber) Like "7" Then
                        TellerTotal(count) = Range("D" & f).Value
                        count = count + 1
                    End If
                    If (BranchNumber) Like "8" Then
                        TellerTotal(count) = Range("E" & f).Value
                        count = count + 1
                    End If
                    If (BranchNumber) Like "9" Then
                        TellerTotal(count) = Range("F" & f).Value
                        count = count + 1
                    End If
                    If (BranchNumber) Like "11" Then
                        TellerTotal(count) = Range("G" & f).Value
                        count = count + 1
                    End If
                    If (BranchNumber) Like "12" Then
                        TellerTotal(count) = Range("H" & f).Value
                        count = count + 1
                    End If
                End If
            Next
        Next f
    End If
    If (BranchNumber) Like "13" Or (BranchNumber) Like "16" Or (BranchNumber) Like "18" Or (BranchNumber) Like "19" Or (BranchNumber) Like "20" Or (BranchNumber) Like "51" Then
        fRow = Sheets(2).Range("A562").End(xlUp).Row

        For f = fRow To 377 Step -1
        Range("A" & f).Select
            For CountDown = 0 To 30 Step 1
                If (Range("A" & f).Value) Like Tellers(CountDown) Then
                    TellerName(count) = Range("B" & f).Value
                    If (BranchNumber) Like "13" Then
                        TellerTotal(count) = Range("C" & f).Value
                        count = count + 1
                    End If
                    If (BranchNumber) Like "16" Then
                        TellerTotal(count) = Range("D" & f).Value
                        count = count + 1
                    End If
                    If (BranchNumber) Like "18" Then
                        TellerTotal(count) = Range("E" & f).Value
                        count = count + 1
                    End If
                    If (BranchNumber) Like "19" Then
                        TellerTotal(count) = Range("F" & f).Value
                        count = count + 1
                    End If
                    If (BranchNumber) Like "20" Then
                        TellerTotal(count) = Range("G" & f).Value
                        count = count + 1
                    End If
                    If (BranchNumber) Like "51" Then
                        TellerTotal(count) = Range("H" & f).Value
                        count = count + 1
                    End If
                End If
            Next
        Next f
    End If
    Dim Counted
    Dim InputCount
    Dim BranchName As String

    If BranchNumber Like "1" Then
        BranchName = "Swinney Teller Report"
        Sheets(3).Activate
    End If
    If BranchNumber Like "2" Then
        BranchName = "Decatur Teller Report"
        Sheets(4).Activate
    End If
    If BranchNumber Like "3" Then
        BranchName = "Tillman Teller Report"
        Sheets(5).Activate
    End If
    If BranchNumber Like "4" Then
        BranchName = "Huntington Teller Report"
        Sheets(6).Activate
    End If
    If BranchNumber Like "5" Then
        BranchName = "Medical Park Teller Report"
        Sheets(7).Activate
    End If
    If BranchNumber Like "6" Then
        BranchName = "West Jefferson Teller Report"
        Sheets(8).Activate
    End If
    If BranchNumber Like "7" Then
        BranchName = "New Haven Teller Report"
        Sheets(9).Activate
    End If
    If BranchNumber Like "8" Then
        BranchName = "Waynedale Teller Report"
        Sheets(10).Activate
    End If
    If BranchNumber Like "9" Then
        BranchName = "Scottsville Teller Report"
        Sheets(11).Activate
    End If
    If BranchNumber Like "11" Then
        BranchName = "Columbia City Teller Report"
        Sheets(12).Activate
    End If
    If BranchNumber Like "12" Then
        BranchName = "Danville Teller Report"
        Sheets(13).Activate
    End If
    If BranchNumber Like "13" Then
        BranchName = "Mattoon Teller Report"
        Sheets(14).Activate
    End If
    If BranchNumber Like "16" Then
        BranchName = "Lima Teller Report"
        Sheets(15).Activate
    End If
    If BranchNumber Like "18" Then
        BranchName = "Stellhorn Crossing Teller Report"
        Sheets(16).Activate
    End If
    If BranchNumber Like "19" Then
        BranchName = "Wayne Haven Teller Report"
        Sheets(17).Activate
    End If
    If BranchNumber Like "20" Then
        BranchName = "Hopkinsville Teller Report"
        Sheets(18).Activate
    End If

        Range("B1").Value = BranchName
        Range("B2").Formula = "=Today()"
        ActiveCell.NumberFormat = "[$-409]mmmm-yy;@"
        Range("A5").Value = "Totals"
        Range("B5").Value = "Name of Employee"
        Range("C5").Value = "Error"
        Range("D5").Value = "Percent"
        Counted = 6
        For InputCount = 0 To 30 Step 1
            If TellerName(InputCount) <> "" Then
                Range("A" & InputCount + 6).Value = TellerTotal(InputCount)
                Range("A" & InputCount + 6).NumberFormat = "#,##0"
                Range("B" & InputCount + 6).Value = TellerName(InputCount)
                Range("D" & InputCount + 6).Formula = "=if(A" & InputCount & "<>0,(A" & InputCount & "-C" & InputCount & ")/A" & InputCount & ",""N/A"")"
                Range("D" & InputCount + 6).NumberFormat = "0.000%"
                Counted = Counted + 1
            End If
        Next
        Range("A" & Counted).Formula = "=sum(A6:A" & Counted & ")"
        Range("B" & Counted).Value = "Total Tellers"
        Range("C" & Counted).Formula = "=sum(C6:C" & Counted & ")"
        Range("D" & Counted).Formula = "=if(A" & Counted & "<>0,(A" & Counted & "-C" & Counted & ")/A" & Counted & ",""N/A"")"
        Range("D" & Counted).NumberFormat = "0.000%"
        Columns("A:D").Select
        Selection.Columns.AutoFit
        Call Format(Counted)
        Call FormatBorder(Counted)
        Range("A1").Select
        Windows(Branch).Activate
        Windows(Branch).Close (False)
Next

结束子

我知道这很长,但这就是全部。我添加了几个用于故障排除的消息框,它们通常不会出现。

发生的事情是这样的: 我提示用户输入分支号,然后从该分支打开文件。我有贯穿文件并从中提取信息的代码。 (此代码使用 for 循环可以正常工作)。根据提取的信息和分行编号,它选择在主工作簿上查找出纳员姓名和总数的位置,然后将所有这些信息插入到由分行编号确定的工作表上。然后重新开始询问下一个分支编号和文件。它一直持续到所有 16 个分支文件都被处理到主文件中。我确信有更快的方法来做这件事,但我还没有得到这个工作,所以我现在不担心优化。我只需要它工作。

【问题讨论】:

  • fRow 的值是多少?什么时候进入那条线?
  • 尝试用“=”替换“喜欢”
  • fRow = 1 因为你只引用一个单元格,我相信。 fRow = ActiveSheet.Cells(Activesheet.Rows.Count, "A").End(xlUp).Row
  • @JoeLaviano 它引用了最后一行的值,不一定 = 1
  • @joelaviano 我不知道为什么它等于 1。我使用过诸如此类的其他语句,但它从未等于 1。它应该等于我在 fRow = 语句中设置的值,在本例中为 188。我无法将其设置为总行数,因为我只希望它搜索前 188 行。其余的稍后搜索。

标签: vba excel


【解决方案1】:

错误在于这一行:

fRow = Sheets(2).Range("A188").End(xlUp).Row

虽然在很多情况下这会获取最后使用的行,但如果从 A1 到 A188 的所有数据都填充了数据,则计算将返回 1,因为这是该范围内的顶部单元格,因为它没有找到空白单元格结束搜索。

根据您的数据的显示方式,有 3 种可能的解决方案:

  1. 如果数据的行数总是超过 188 行,则将 fRow 设置为 188,而无需进行计算
    fRow = 188
  2. 如果数据没有空缺,从上往下,从上往下数
    fRow = Sheets(2).Range("A1").End(xldown).Row
    然后查看是否大于188
  3. 如果有间隙,则从最后可能的行开始搜索
    fRow = Sheets(2).cells(sheets(2).Usedrange.Rows.Count+1,1).End(xlUp).Row
    并检查是否超过188,否则设置为188

【讨论】:

  • 就是这样。因为 A 列中的数据没有间隙,所以它总是返回 1。我将 fRow 设置为 188,现在它可以工作了。谢谢!
【解决方案2】:

您是否检查过 fRow 的值。可能是循环开始时变量fRow小于变量f,所以启动循环的条件永远不会满足。

【讨论】:

    【解决方案3】:

    请注意,变量 fRow 假定列中第一个非空单元格的行值fRow = Sheets(2).Range("A188").End(xlUp).Row

    因此,fRow 的值介于 1 到 188 之间,并且您的循环语句 For f = fRow To 3 Step -1 仅在 fRow 大于 3 时才有效。

    请在 fRow 进入您的循环之前检查它的值。

    另外,IF BranchNumber 只能是 1,2,3,4 或 5,当你已经知道它是 1 时,你不必检查它是否是 2....使用 Else条件:

    If (BranchNumber) Like "1" Then
       TellerTotal(count) = Range("C" & f).Value
       MsgBox (TellerTotal(count))
       count = count + 1
       ElseIf (BranchNumber) Like "2" Then
            TellerTotal(count) = Range("E" & f).Value
            MsgBox (TellerTotal(count))
             count = count + 1
       End If      
    End If
    

    【讨论】:

    • 你说的对我来说没有意义。我还有其他类似的语句,其中For f = fRow To 3 Step -1 和 fRow 从我在fRow = 语句中设置的位置开始,在这种情况下为 188。然后 f 会倒数直到达到 3。(有 0 个空白'A188:A3' 范围内的单元格)
    • 因此,如果 A188:A3 范围内有 0 个空白单元格,您的 fRow 将为 3
    猜你喜欢
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多