使用VBA将循环嵌套在IF语句中

时间:2013-10-23 12:25:37

标签: excel vba excel-vba

我有一个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

End Sub

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

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

3 个答案:

答案 0 :(得分:0)

你检查了fRow的价值吗?当循环开始时,变量fRow可能小于变量f,因此永远不会满足启动循环的条件。

答案 1 :(得分:0)

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

因此,fRow的值为1到188,只有当fRow大于3时,循环语句For f = fRow To 3 Step -1才会起作用。

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

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

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

答案 2 :(得分:0)

错误在于这一行:

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

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

有三种可能的解决方案,具体取决于数据的显示方式:

  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