[英]VBA Looping through multiple worksheets, getting End If w/o block error
我正在尝试创建一个遍历40个工作表并在两个日期之间的工作表中添加值的函数。 运行该函数时,出现“ End If Without Block If”错误。 当我删除最后一个End If If时,出现“无效的下一个控制变量引用”错误。 任何帮助将不胜感激。 谢谢!
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循环中缺少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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.