Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在vba中将字符串作为公式计算_Vba_Excel - Fatal编程技术网

在vba中将字符串作为公式计算

在vba中将字符串作为公式计算,vba,excel,Vba,Excel,我试图计算excel单元格,其中包含如下数据: “AAA*2” 然后在vba中尝试使用 dim AAA dim equation dim result AAA=inputbox("?") 'some numeric value equation=sheets("sheet1").range("A1").value 'contains "AAA*2" result=application.evaluate("=" & equation)

我试图计算excel单元格,其中包含如下数据:

“AAA*2”

然后在vba中尝试使用

    dim AAA
    dim equation
    dim result
    AAA=inputbox("?") 'some numeric value
    equation=sheets("sheet1").range("A1").value 'contains "AAA*2"
    result=application.evaluate("=" & equation) 

我不知道这样做是否可能。如果可能的话,这将使我当前的项目更加简单。感谢您事先提供的帮助。

您不能将vba和字符串混合使用

添加
公式=替换(公式“AAA”,AAA)


不能混合使用vba和字符串

添加
公式=替换(公式“AAA”,AAA)


声明您的类型,并注意您对所涉及的值所做的隐含假设

Dim userInput As Variant
userInput = InputBox("?") ' could be numeric, could be a null string pointer, could be anything.

If Not IsNumeric(userInput) Then Exit Sub

Dim AAA As Double
AAA = CDbl(userInput)

Dim source As Worksheet
Set source = ThisWorkbook.Worksheets("Sheet1")

Dim sourceEquation As Variant
sourceEquation = source.Range("A1").Value ' could be an error, an empty variant, a date, whatever

If IsError(sourceEquation) Then Exit Sub

Dim equation As String
'the "AAA" in the string is a string literal, doesn't refer to this local AAA variable.
equation = VBA.Strings.Replace(CStr(sourceEquation), "AAA", AAA)

Dim result As Double
result = Application.Evaluate("=" & equation)

声明您的类型,并注意您对所涉及的值所做的隐含假设

Dim userInput As Variant
userInput = InputBox("?") ' could be numeric, could be a null string pointer, could be anything.

If Not IsNumeric(userInput) Then Exit Sub

Dim AAA As Double
AAA = CDbl(userInput)

Dim source As Worksheet
Set source = ThisWorkbook.Worksheets("Sheet1")

Dim sourceEquation As Variant
sourceEquation = source.Range("A1").Value ' could be an error, an empty variant, a date, whatever

If IsError(sourceEquation) Then Exit Sub

Dim equation As String
'the "AAA" in the string is a string literal, doesn't refer to this local AAA variable.
equation = VBA.Strings.Replace(CStr(sourceEquation), "AAA", AAA)

Dim result As Double
result = Application.Evaluate("=" & equation)

非常感谢你的回答。最简单的事情,我无法思考。:)这将节省我很多时间。@canerkorkmaz这个(和你的)代码所做的假设将在某一天或另一天从后面咬到你。@canerkorkmaz我同意Mat'sMug。我尽了最小的努力来解决这个问题,但是他的,实际上解决了所有的问题。事实上,我写了上面的代码,这是最简单的提问方式。当然,我在实际项目中声明了我的类型。但是在@Mat'sMug的回答中,我知道我也可以声明工作表。所以谢谢你。非常感谢你的回答。最简单的事情,我无法思考。:)这将节省我很多时间。@canerkorkmaz这个(和你的)代码所做的假设将在某一天或另一天从后面咬你。@canerkorkmaz我同意Mat'sMug。我尽了最小的努力来解决这个问题,但是他的,实际上解决了所有的问题。事实上,我写了上面的代码,这是最简单的提问方式。当然,我在实际项目中声明了我的类型。但是在@Mat'sMug的回答中,我知道我也可以申报工作表。所以谢谢你。谢谢你的回答。我理解你的意思,我会在心里写代码。谢谢你的回答。我理解你的意思,我会在心里写代码。