我在Excel VBA中进行数值积分,我想从用户那里获得公式,而不是将它插入到单元格中。使用数学公式输入值在VBA中进行数学计算Exccel
例
用户给我的x^3 + X^2 + 2是F(X)
在A1,我有号码2和I在B1评价F(X)。
如何告诉excel从用户输入的公式是= A1^3 + A2^2 + 2。我只需要转换到一个单元格。
我使用瓦尔(的InputBox())
感谢您的帮助
我在Excel VBA中进行数值积分,我想从用户那里获得公式,而不是将它插入到单元格中。使用数学公式输入值在VBA中进行数学计算Exccel
例
用户给我的x^3 + X^2 + 2是F(X)
在A1,我有号码2和I在B1评价F(X)。
如何告诉excel从用户输入的公式是= A1^3 + A2^2 + 2。我只需要转换到一个单元格。
我使用瓦尔(的InputBox())
感谢您的帮助
的Application Evaluate功能可以解析公式从用户接收,但你必须找到某种方式来X转换为可以正确理解的单元格引用。这可能需要工作表名称。
您的x^3 + x^2 + 2示例用A1和A2代替两个x值。这可能会更好,因为在A1和A2之间没有歧义。
Sub f_of_x_and_y()
Dim fmla As String, str As String
fmla = InputBox("enter formula")
With Worksheets("Sheet2")
str = Replace(Replace(fmla, "x", .Range("A1").Address(external:=True)), _
"y", .Range("A2").Address(external:=True))
.Range("A3") = Application.Evaluate(str)
End With
End Sub
如果用户写入exp(x)+ 2 * x – Maxwell
如果用户输入X或X,你只需要与A1.cell.value更换X然后使用此:
Sub test()
formula_user = InputBox("Please type formula")
Range("B1").Formula = "=" & Replace(LCase(formula_user), "x", "A1")
End Sub
x是从所述用户输入框式与A1替换。在用户输入中可以是大写或小写
Sub variable_input()
Dim userFormula$
Dim myVar&
myVar = 2
userFormula = InputBox("What is the formula?")
Debug.Print userFormula
userFormula = Replace(userFormula, "x", myVar)
Debug.Print userFormula
Dim theAnswer$
theAnswer = Val(userFormula)
Debug.Print theAnswer
End Sub
谢谢!其实这一个将帮助用户看到他写的公式 – Maxwell
您可以用字符串“A1”替换所有“x”。不要忘记在前面添加“=”...
Sub TestUserEquation()
Dim strUserEquation As String
strUserEquation = LCase(InputBox("Please enter your equation:", "Equation InputBox"))
If strUserEquation <> "" Then
Cells(1, 2) = "=" & Replace(strUserEquation, "x", "A1")
Else
Cells(1, 2) = "No equation entered."
End If
End Sub
感谢Porcupine911 – Maxwell
到目前为止你有什么代码?请发布它,以便a)如果您已经有代码,我们不会重新发明轮子,b)向我们展示您尝试过的内容,以及c)可以找到任何潜在的错误。我不明白这应该如何,因为你在VBA中,只需将变量插入用户提供的公式中,不是吗? – BruceWayne
你为什么不去做每个数学老师在学校告诉我们的东西,用'A1'中的数字代替'x'。像'Replace(“x^3 + x^2 + 2”,“x”,Range(“A1”)。Value)'。 – Ralph
在星期天的早晨,VBA中的数学方程没有什么问题,让人们都兴奋起来! – BruceWayne