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

我试图创build一个函数,循环通过40个工作表,并添加两个date之间的工作表中的值。 当我运行该函数,我得到一个“如果没有块如果”错误。 当我删除最后结束如果然后我得到一个“无效的下一个控制variables引用”错误。 任何帮助将不胜感激。 谢谢!

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 

您在第二个For循环中缺lessNext语句

 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