當前位置:成語大全網 - 新華字典 - vba中用if判斷多個條件,符合其中壹個就跳出

vba中用if判斷多個條件,符合其中壹個就跳出

可以用if elseif else,也可以用select case,示例代碼如下:

(1)if elseif;

Sub?if_sample()

Dim?i?As?Integer

For?i?=?1?To?300

If?i?=?1?Then

Debug.Print?"i值等於1"

ElseIf?i?=?20?Then

Debug.Print?"i值等於20"

ElseIf?i?=?40?Then

Debug.Print?"i值等於40"

ElseIf?i?=?100?Then

Debug.Print?"i值等於100"

ElseIf?i?=?300?Then

Debug.Print?"i值等於300"

End?If

Next?i

End?Sub

(2)select case;

Sub?select_sample()

Dim?i?As?Integer

For?i?=?1?To?300

Select?Case?i

Case?1:

Debug.Print?"i值等於1"

Case?20:

Debug.Print?"i值等於20"

Case?40:

Debug.Print?"i值等於40"

Case?100:

Debug.Print?"i值等於100"

Case?300:

Debug.Print?"i值等於300"

End?Select

Next?i

End?Sub