Vba 嵌套if的问题

Vba 嵌套if的问题,vba,nested-if,Vba,Nested If,请任何人指出我在下面给出的代码中犯了什么错误,我得到了编译错误,对于每个EndIF都必须有相应的IF,提前感谢: Public Function customavg(rng As Range, nr_weeks As Integer) Dim total As Integer, count_rng_row As Integer, count_wk As Integer, counter_rng As Integer total = 0 count_rng_row = rng.Rows.coun

请任何人指出我在下面给出的代码中犯了什么错误,我得到了编译错误,对于每个EndIF都必须有相应的IF,提前感谢:

Public Function customavg(rng As Range, nr_weeks As Integer)
Dim total As Integer, count_rng_row As Integer, count_wk As Integer, counter_rng As Integer

total = 0
count_rng_row = rng.Rows.count
count_wk = 0
counter_rng = count_rng_row


   For counter_rng = count_rng_row To 1
     If count_wk < nr_weeks Then
        If rng.Cells.Offset(0, -1) = "b" Then total = total + rng.Cells.Value
           counter_rng = counter_rng - 1
           count_wk = count_wk + 1
        End If
     'Else
      '     counter_rng = counter_rng - 0
       '    count_wk = count_wk + 0
     End If
   Next counter_rng

customavg = total / nr_weeks

End Function
公共函数customavg(rng作为范围,nr_周作为整数)
Dim total为整数,count_rng_row为整数,count_wk为整数,counter_rng为整数
总数=0
count\u rng\u row=rng.Rows.count
count_wk=0
计数器=计数行
对于计数器\u rng=计数\u rng\u行为1
如果计数周
罪魁祸首是

If rng.Cells.Offset(0, -1) = "b" Then total = total + rng.Cells.Value
If
的语法为

If <cond> Then <command>
If-Then

If-Then
如果结束
罪魁祸首是

If rng.Cells.Offset(0, -1) = "b" Then total = total + rng.Cells.Value
If
的语法为

If <cond> Then <command>
If-Then

If-Then
如果结束

正确。您只需将
total=total+rng.Cells.Value
放在新行上即可。感谢您的回复。。我还有一个疑问。。循环是否可以按降序运行,例如在上面的代码中。。“For counter\u rng=count\u rng\u row To 1”因为我需要取最新的求和值,所以计数器将从底部单元格到顶部。这是一个完全不同的问题,但这里是:
For c=9到1步骤-1
。正确。您只需将
total=total+rng.Cells.Value
放在新行上即可。感谢您的回复。。我还有一个疑问。。循环是否可以按降序运行,例如在上面的代码中。。“For counter\u rng=count\u rng\u row To 1”因为我需要取最新的求和值,所以计数器将从底部单元格到顶部。这是一个完全不同的问题,但这里是:
For c=9到1步骤-1