我正在尝试使用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 "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

最佳答案

创建一个字符串并使用Application.Evaluate:

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

关于excel - 从InputBox获取数学运算符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59181541/

10-12 12:29