VBA循环通过多个工作表,获得结束如果没有块错误

时间:2015-05-06 15:39:36

标签: excel vba excel-vba

我正在尝试创建一个循环遍历40个工作表的函数,并在两个日期之间的工作表中添加值。当我运行该函数时,我得到一个“结束如果没有阻止如果”错误。当我删除最后一个结束时,我得到一个“无效的下一个控制变量引用”错误。任何帮助将不胜感激。谢谢!

Function addIntPrinc(beginDate, endDate)

Dim ws As Worksheet
Dim finalRow As Long, I As Long, intPrinc As Double
intPrinc = 0
finalRow = Cells(Rows.Count, 1).End(xlUp).Row

For Each ws In Worksheets
    If ws.Name Like "Loan *#" Then
        For I = 25 To finalRow
        If Cells(I, 2) >= beginDate And Cells(I, 2) < endDate Then
            intPrinc = intPrinc + Cells(I, 3).Value
        End If
    End If
Next ws
End Function

addIntPrinc = intPrinc

1 个答案:

答案 0 :(得分:2)

你在第二个For循环中缺少Next语句

For Each ws In Worksheets
    If ws.Name Like "Loan *#" Then
        For I = 25 To finalRow
             If Cells(I, 2) >= beginDate And Cells(I, 2) < endDate Then
                 intPrinc = intPrinc + Cells(I, 3).Value
            End If
        Next '***New Line
    End If
Next ws