循环vba中的多个if语句

循环vba中的多个if语句,vba,excel,Vba,Excel,我正在尝试运行一个简单的if语句,但无法让它为我运行而不出错。基本上,我试图在一个循环中有多个if语句,我猜我有一个小错误,但无法发现,可能是else语句。谢谢你的帮助 Sub ex13() Dim rgtimetable As Range, rgR As Range, counter As Integer Dim counter1 As Integer, counter2 As Integer, counter3 As Integer Set rgtimetable = Range("ti

我正在尝试运行一个简单的if语句,但无法让它为我运行而不出错。基本上,我试图在一个循环中有多个if语句,我猜我有一个小错误,但无法发现,可能是else语句。谢谢你的帮助

Sub ex13()

Dim rgtimetable As Range, rgR As Range, counter As Integer
Dim counter1 As Integer, counter2 As Integer, counter3 As Integer

Set rgtimetable = Range("timetable")
For Each rgR In rgtimetable
    rgR.Activate
    If classyear(ActiveCell.Value) = 0 Then counter = counter + 1   ' classyear is a function i am calling from above

    Else
    If classyear(ActiveCell.Value) = 1 Then counter = counter1 + 1

    Else
    If classyear(ActiveCell.Value) = 2 Then counter = counter2 + 1

    Else
    If classyear(ActiveCell.Value) = 3 Then counter = counter3 + 1

Next rgR

MsgBox counter
MsgBox counter1
MsgBox counter2
MsgBox counter3

End Sub

在VBA中,有几种方法可以编写
If
语句:

If [some condition] Then [Do something]

或者,最后

If [some condition] Then
   [Do something]
ElseIf [some other condition] Then
   [Do something different]
Else
   [Do something else]
End If
在代码中,您的
If
语句都在一行上,因此不需要相应的
End If
,但也不能在下一行上使用相应的
Else
语句。如果要使用
Else
ElseIf
,则必须使用最终的
If
语句块模式,并使用相应的
EndIf
关闭
If

在您的情况下,由于您总是测试相同的东西(
classyear(ActiveCell.Value)
),我建议利用
selectcase
结构,这将缩短您的代码

Select Case classyear(ActiveCell.Value)
Case 0
   counter = counter + 1   ' classyear is a function i am calling from above

Case 1
   counter = counter1 + 1

Case 2
   counter = counter2 + 1

Case 3
   counter = counter3 + 1

Case Else
   'Oops, this shouldn't happen, but handle the error anyway

End Select

在VBA中,有几种方法可以编写
If
语句:

If [some condition] Then [Do something]

或者,最后

If [some condition] Then
   [Do something]
ElseIf [some other condition] Then
   [Do something different]
Else
   [Do something else]
End If
在代码中,您的
If
语句都在一行上,因此不需要相应的
End If
,但也不能在下一行上使用相应的
Else
语句。如果要使用
Else
ElseIf
,则必须使用最终的
If
语句块模式,并使用相应的
EndIf
关闭
If

在您的情况下,由于您总是测试相同的东西(
classyear(ActiveCell.Value)
),我建议利用
selectcase
结构,这将缩短您的代码

Select Case classyear(ActiveCell.Value)
Case 0
   counter = counter + 1   ' classyear is a function i am calling from above

Case 1
   counter = counter1 + 1

Case 2
   counter = counter2 + 1

Case 3
   counter = counter3 + 1

Case Else
   'Oops, this shouldn't happen, but handle the error anyway

End Select

counter=counter1+1
应该是
counter1=counter1+1
(etc)@RC。不,当我尝试一个如果时,计数器为我工作,当我引入其他如果时,我的计数器出错。你的评论(回答)“我仍然有一个计数器的问题,但不确定它是什么。我看看我是否能解决它”-参见@RC的评论。
counter=counter1+1
可能应该是
counter1=counter1+1
(etc)@RC。不,当我尝试一个if时,计数器为我工作,当我引入其他if时,我的计数器出错。你的评论(回答)“我仍然对计数器有问题,但不确定是什么问题。我看看我是否能解决它”-见@RC的评论。嘿,非常感谢你的深入回答,读过之后,我现在对它有了更好的理解。我的计数器仍然有问题,但不确定是什么问题。我看看能不能解决it@leonHill我认为你的问题可能是counter1、counter2和counter3从未设置为任何值。你还有什么错误吗?@Steves是的,你的真命天子我忘了把计数器设为1,2和3。我设置了一半,但忘了做另一半。最后一个问题是,如果我想让计数器的值显示在单元格中而不是消息框中,你知道我该怎么做吗?@YowE3K,我已将其添加到:)@leonHill-你真的应该在新问题中进一步提问。嘿,非常感谢你的深入回答,读过之后,我现在对它有了更好的理解。我的计数器仍然有问题,但不确定是什么问题。我看看能不能解决it@leonHill我认为你的问题可能是counter1、counter2和counter3从未设置为任何值。你还有什么错误吗?@Steves是的,你的真命天子我忘了把计数器设为1,2和3。我设置了一半,但忘了做另一半。最后一个问题是,如果我想让计数器的值显示在单元格中而不是消息框中,你知道我该怎么做吗?谢谢@YowE3K,我已将其添加到:)@leonHill-你真的应该在新问题中进一步提问。