Today's Goals

  • Master If...Then...Else
  • Master Select Case
  • Nest conditions correctly

Key Concepts

1. If...Then...Else

Conditionals let the program act according to the situation. The basic form:

vba
If condition Then ' runs when the condition is true Else ' runs when the condition is false End If

Chain extra tests with ElseIf, and combine conditions with And and Or. Branch order decides the result: VBA tests top-down and exits at the first match, so range checks must run from high to low — otherwise 85 hits score >= 60 first and reads "Pass".

2. Select Case

One variable checked against several fixed values reads cleaner as Select Case than as a long ElseIf chain:

vba
Select Case variable Case value1 ' handle value1 Case value2 ' handle value2 Case Else ' everything else End Select

Case supports ranges: Case Is >= 90, Case 1 To 10. Rule of thumb: Select Case for discrete values (grades, departments), If...ElseIf for ranges and combined conditions.

3. Nested Conditions

An If inside another If expresses "A first, then B" (the .bas example checks age, then student status). Past two levels, flatten the logic with And.

Today's Code

vba
Sub If示例() Dim score As Integer score = 85 If score >= 90 Then MsgBox "优秀" ElseIf score >= 80 Then MsgBox "良好" ElseIf score >= 60 Then MsgBox "及格" Else MsgBox "不及格" End If End Sub Sub SelectCase示例() Dim grade As String grade = "A" Select Case grade Case "A" MsgBox "优秀" Case "B" MsgBox "良好" Case "C" MsgBox "中等" Case Else MsgBox "未知等级" End Select End Sub

In the If example, 85 lands in the score >= 80 branch; in the Select Case example, "A" hits the first Case.

Real-World Scenario

The setup: tiered commission — 3% on sales up to 100,000, 5% from 100,000 to 200,000, 8% above 200,000:

vba
Sub 计算提成() Dim 销售额 As Double, 提成率 As Double 销售额 = 150000 If 销售额 > 200000 Then 提成率 = 0.08 ElseIf 销售额 > 100000 Then 提成率 = 0.05 Else 提成率 = 0.03 End If MsgBox "提成: " & 销售额 * 提成率 & " 元" End Sub

150,000 takes the second branch and pops up "提成: 7500 元". Conditions must run largest-first, or deals over 200,000 stop at 5% — no error, just money quietly lost every month.

Exercises

  1. Assign a letter grade from a score
  2. Decide whether a given year is a leap year
  3. Suggest a reward or penalty based on a score

Exercise Solutions

  1. Test the score ranges with If...ElseIf or Select Case (90 and above Excellent, 80–89 Good, and so on); feed score from an InputBox return value to make it interactive.
  2. Leap-year test: (Year Mod 4 = 0 And Year Mod 100 <> 0) Or (Year Mod 400 = 0) — Mod gives the remainder, an If reports the result.
  3. Replace each branch's MsgBox text with a reward (dinner with friends) or a penalty (extra review).

Extension Exercises

  1. Rewrite the If example as a Select Case version using Case Is >= 90 range syntax.
  2. Print the largest of three numbers using only If.

Common Errors and Troubleshooting

Error / Symptom Cause Fix
Compile error: Block If without End If A multi-line If missing End If, or ElseIf written as "Else If" One End If per block If
Comparisons always fall through to Case Else Leading/trailing spaces, so "A " ≠ "A" Trim() before comparing
An 85 gets graded "Pass" A broad condition ahead of narrower ones Order range checks from high to low

Summary

If...ElseIf for ranges and combined conditions; Select Case for one variable with fixed values. Ship only after checking End If pairs and branch order.


由在线工具箱(www.vba.net)整理制作