VBA函数避免if语句 [英] VBA Function Avoiding If Statements

查看:42
本文介绍了VBA函数避免if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个非常复杂的VBA工作簿,而运行大量代码的问题之一就是性能.我有一个内置的函数,或多或少地执行以下操作

I'm building a pretty complicated VBA workbook, and one of the issues on running much of the code is performance. I have a built in function that does, more or less, the following

Public Function zzz (xxx as String) as String
if xxx = "apple" then zzz = "orange"
if xxx = "appple2" then zzz = "orange2"
if xxx = "apple3" then zzz = "apple3"

等(但大约有30个字符串).我多次调用此函数.有更好的方法吗?

etc. (but with about 30 strings instead). I call this function multiple times. Is there a better way to do this?

推荐答案

如果仅检查一个变量的值,则可以尝试使用Select Case.这样会更快,因为一旦找到正确的变量,它将跳过剩余的行.

You could try using Select Case, if you are only checking the value of one variable. That will be faster, because it will skip the remaining lines once it finds the correct variable.

Select case xxx
case "apple"
zzz = "orange"
case "apple2"
zzz = "orange2"
case "apple3"
zzz = "orange3"
End Select

这篇关于VBA函数避免if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆