最近有个同事需要编写个宏,Excel-VBA中需要用到类似php中的swith-case的功能,翻了下,将VBA中类似的示例代码列出来供需要的人参考:

Select Case income
Case Is <= 0
profitrank = "<=0"
Case Is <= 5000
profitrank = "0-5千万"
Case Is <= 10000
profitrank = "5千万-1亿"
Case Is <= 20000
profitrank = "1亿-2亿"
Case Is <= 30000
profitrank = "2亿-3亿"
Case Is <= 40000
profitrank = "3亿-4亿"
Case Is <= 50000
profitrank = "4亿-5亿"
Case Is <= 100000
profitrank = "5亿-10亿"
Case Else
profitrank = ">10亿"
End Select

上述代码是判定值的,注意顺序。还有个办法直接判定外部的值。

Select Case true
Case Target.Row > 3 And Target.Row < 10
rem 4-9行
Case Target.Row > 10 And Target.Row < 17
rem 11-16行
Case Else
rem 其他行
End Select

Related Posts: Excel-VBA-switch的示例 :