转自淘财财的博客
“可惜的是excel工作表竟然不知道switch函数,但是老天有眼,excel的VBA函数却可以支持switch,所以有些高手就做了些为国为名的好事”
调用形式
=Switch2(A1<1000,”D”,A1<5000,”C”,A1<10000,”B”,TRUE,”A”)
VBA代码:
Function Switch2(Test1 As String, Result1 As String, _
Optional Test2 As String, Optional Result2 As String, _
Optional Test3 As String, Optional Result3 As String, _
Optional Test4 As String, Optional Result4 As String, _
Optional Test5 As String, Optional Result5 As String, _
Optional Test6 As String, Optional Result6 As String, _
Optional Test7 As String, Optional Result7 As String, _
Optional Test8 As String, Optional Result8 As String, _
Optional Test9 As String, Optional Result9 As String, _
Optional Test10 As String, Optional Result10 As String, _
Optional Test11 As String, Optional Result11 As String, _
Optional Test12 As String, Optional Result12 As String, _
Optional Test13 As String, Optional Result13 As String, _
Optional Test14 As String, Optional Result14 As String)
Switch2 = Switch(Test1, Result1, _
Test2, Result2, _
Test3, Result3, _
Test4, Result4, _
Test5, Result5, _
Test6, Result6, _
Test7, Result7, _
Test8, Result8, _
Test9, Result9, _
Test10, Result10, _
Test11, Result11, _
Test12, Result12, _
Test13, Result13, _
Test14, Result14)
End Function
以Office 2010为例,Alt+F11打开VBA编辑器,点插入>模块可以添加自定义函数。
需要返回整数值的时候,把所有的Result As String替换成Result As Integer。