vba 循环vba中的多个if语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43225840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:24:01  来源:igfitidea点击:

Multiple if statements in a loop vba

vbaexcel-vbaexcel

提问by leon Hill

I am trying to run a simple if statement but can not get it to run for me without getting an error. Basically, I am trying to have multiple if statements within a loop, im guessing i have a minor error but cannot spot it, maybe with the else statements. Any help is appreciated

我正在尝试运行一个简单的 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

回答by SteveES

In VBA there are several ways to write an Ifstatement:

在 VBA 中有几种写If语句的方法:

If [some condition] Then [Do something]

Or

或者

If [some condition] Then [Do something] Else [Do something else]

Or

或者

If [some condition] Then
   [Do something]
End If

Or

或者

If [some condition] Then
   [Do something]
Else
   [Do something else]
End If

Or, finally

或者,最后

If [some condition] Then
   [Do something]
ElseIf [some other condition] Then
   [Do something different]
Else
   [Do something else]
End If

In your code your Ifstatements are all on one line and therefore don't need a corresponding End If, but also can't take a corresponding Elsestatement on a following line. If you want to use an Elseor an ElseIf, you must use the final Ifstatement block pattern and close the Ifblock with a corresponding EndIf.

在您的代码中,您的If语句都在一行上,因此不需要相应的End If,但也不能Else在下一行中使用相应的语句。如果要使用 anElse或 an ElseIf,则必须使用 finalIf语句块模式并If用相应的关闭块EndIf

In your case, as you are always testing the same thing (classyear(ActiveCell.Value)), I would advise taking advantage of the Select Caseconstruction, which will shorten your code.

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

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