请知悉:本文最近一次更新为 9年 前,文中内容可能已经过时。

最近有个同事需要编写个宏,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

如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。

尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。


与《Excel-VBA-switch的示例》相关的博文:


留言

avatar
😀
😀😁😂😅😭🤭😋😘🤔😰😱🤪💪👍👎🤝🌹👌