gpt4 book ai didi

excel - 从InputBox获取数学运算符

转载 作者:行者123 更新时间:2023-12-02 23:48:07 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com