excel - 从InputBox获取数学运算符

标签 excel vba

我正在尝试使用 InputBox 来获取数学运算符

例如:+。当我用变量替换运算符时,会出现错误

Expected Then or GoTo

这不起作用:

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/

相关文章:

c# - 在 Excel、EPPlus、C# 中以编程方式隐藏选项卡

vba - 如何使用 VBA 检测 Excel 电子表格中是否按 ctrl + Enter

VBA用其计算值替换函数公式内的范围

vba - 如何在VBA中创建一个返回对象列表的函数

excel - 在 Microsoft Office 中预填充调色板颜色?

ms-access - MS ACCESS 通过查询获取 "Table Description"

python - 使用python删除excel文件中的NA行

python - 无法获取 Excel ChartObjects 计数

Excel CSV。包含超过 1,048,576 行数据的文件

c++ - 如何通过 COM 遍历 Excel 2003 工作表中的所有分页符