VBA是否有三元运算符? [英] Does VBA Have a Ternary Operator?
问题描述
我来自美丽的Obj C世界,它基于C编程语言,并且我爱上了寻找节省空间的古怪方法.但是,我已经浏览了尽可能多的文档,并且在VBA上找不到任何能缩短此语法的内容:
I come from the beautiful world of Obj C, which is based on the C programming language, and I've fallen in love with finding quirky ways to save space. However, I've looked through as much documentation as I can and I can't find anything juicy on VBA that will shorten this syntax:
If boolVar = True Then
'Do something
Else
'Do nothing
End If
在Obj C(当然还有C)中,我对此非常熟悉:
In Obj C, and naturally C, I'm extremely familiar with doing this:
boolVar ? "Nope, tis false" : "Yup, tis true"
这与大多数其他语言所使用的语言非常相似,有些语言可能会使用诸如!=
或==
之类的额外逻辑运算符,但我对此持乐观态度.如果是这样的话,我可能没有找对地方,请让我知道,您可以从哪里获取文件.
This is very similar to what most other languages use, some may use extra logical operators like !=
or ==
but that leaves me optimistic. I may not have looked in the right places, if that's the case PLEASE let me know where you get your documentation.
TLDR,我们可以将If/Then/Else缩短为VBA中的一行代码吗? 这非常方便,当"Do Somethings"仅用于设置另一个变量的单个参数或启用/禁用按钮时.
TLDR, can we shorten If/Then/Else to one line of code in VBA? This is extremely handy when the 'Do Somethings' are nothing more then setting another variable's single parameter, or enabling/disabling a button.
推荐答案
Sub test()
Dim x As Long
Dim y As Long
y = 1
x = IIf(y = 1, 1, 2)
End Sub
这篇关于VBA是否有三元运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!