Excel 从InputBox获取数学运算符
我正在尝试使用InputBox获得一个数学运算符 例如:Excel 从InputBox获取数学运算符,excel,vba,Excel,Vba,我正在尝试使用InputBox获得一个数学运算符 例如:+。当我用变量替换运算符时,它会给出一个错误 那就去吧 这不起作用: Sub NumTest() Dim Val As Integer Val = Application.InputBox("Value", "Value", Type:=1) oper = Application.InputBox("Op") rcell = Range("D2") If rcell oper Val = True Then MsgBox "Tr
+
。当我用变量替换运算符时,它会给出一个错误
那就去吧
这不起作用:
Sub NumTest()
Dim Val As Integer
Val = Application.InputBox("Value", "Value", Type:=1)
oper = Application.InputBox("Op")
rcell = Range("D2")
If rcell oper Val = True Then
MsgBox "True"
End If
End Sub
Sub NumTest()
Dim Val As Integer
Val = Application.InputBox("Value", "Value", Type:=1)
oper = Application.InputBox("Op")
rcell = Range("D2")
If rcell > Val = True Then
MsgBox "True"
End If
End Sub
这确实有效:
Sub NumTest()
Dim Val As Integer
Val = Application.InputBox("Value", "Value", Type:=1)
oper = Application.InputBox("Op")
rcell = Range("D2")
If rcell oper Val = True Then
MsgBox "True"
End If
End Sub
Sub NumTest()
Dim Val As Integer
Val = Application.InputBox("Value", "Value", Type:=1)
oper = Application.InputBox("Op")
rcell = Range("D2")
If rcell > Val = True Then
MsgBox "True"
End If
End Sub
创建字符串并使用应用程序。评估:
Sub NumTest()
Dim Val As Integer
Val = Application.InputBox("Value", "Value", Type:=1)
Dim oper As String
oper = Application.InputBox("Op")
Dim rcell As String
rcell = ActiveSheet.Range("D2")
If Application.Evaluate(rcell & oper & Val) Then
MsgBox "True"
Else
MsgBox "False"
End If
End Sub
运算符是语法语言元素,它们不可争夺:您不能仅仅决定使用某个标识符/字符串变量,并且以其字符串内容为某个运算符的名义,将其视为运算符:从语法上讲,它是一个变量,而不是运算符 因此编译器不知道您在这里的意思:
If rcell oper Val = True Then
因为表达式rcell oper Val=True
在语法上没有意义:
If variable1 variable2 variable3 = True Then
请注意,=True
部分是多余的:布尔表达式只能是True
或False
;如果表达式为True
,则如果将其与文本值True
进行比较,则不会更真实
使用Application.Evaluate
将有效,只要您愿意让Excel完成这项工作。如果您的代码需要移植到任何其他VBA主机应用程序,那么您不能这样做。当然可以,但是引用Excel对象模型只是为了利用它的计算引擎来计算简单的表达式可能有些过分
您可以做的是将合法的oper
值映射到函数。为此制作一些ExpressionEvaluator
类模块
Public Function AddOperator(ByVal LHS As Variant, ByVal RHS As Variant) As Variant
AddOperator = LHS + RHS ' let VBA deal with type mismatches & other issues
End Function
Public Function SubtractOperator(ByVal LHS As Variant, ByVal RHS As Variant) As Variant
SubtractOperator = LHS - RHS ' let VBA deal with type mismatches & other issues
End Function
Public Function LessThanOperator(ByVal LHS As Variant, ByVal RHS As Variant) As Boolean
LessThanOperator = LHS < RHS ' let VBA deal with type mismatches & other issues
End Function
'...
Public Property Get BinaryOperatorMap() As Scripting.Dictionary
Static map As Scripting.Dictionary
If map Is Nothing Then
Set map = New Scripting.Dictionary
map.Add "+", "AddOperator"
map.Add "-", "SubtractOperator"
map.Add "<", "LessThanOperator"
'...
End If
Set BinaryOperatorMap = map
End Property
注意,
CallByName
需要一个对象实例;这就是为什么需要在类模块中实现这些操作。如果应用程序.Evaluate(rcell&oper&Val),则考虑使用工作表.Evaluate
方法,在该方法中可以指定计算公式的上下文Application.Evaluate
使用活动工作表,该工作表现在可能满足您的需要,但稍后会返回给您……rng.Parent.Evaluate(…)
将确保Excel使用正确的工作表作为单元格值的源来计算结果。@FreeSoftwareServers他说使用:如果是活动工作表。Evaluate(rcell&oper&Val)然后改为
。如果引用单元格,则使用工作表。@FreeSoftwareServers如果使用rng.Parent.Evaluate(…)
无法解决当前问题,请提出一个显示实际代码的新问题。@FreeSoftwareServers然后将声明从Integer
更改为Double