Excel VBA - 如何在没有公式的情况下插入纯值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5815314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:24:46  来源:igfitidea点击:

Excel VBA - How To Insert Plain Value without Formula?

excelvbaexcel-vbaexcel-2003

提问by reforrer

I have skimmed many Excel VBA topics, however, I have not found any information about inserting plain values - the result of SUM()in this case - not formula. There is a method, PasteSpecial xlPasteValues, but it seems that it is not what I am looking for.

我浏览了许多 Excel VBA 主题,但是,我没有找到任何有关插入普通值的信息 -SUM()在这种情况下是结果- 而不是公式。有一种方法,PasteSpecial xlPasteValues但似乎不是我要找的。

The following code example inserts the formula, not only the value:

以下代码示例插入公式,而不仅仅是值:

Sub test()
    Range("A4").Value = "=SUM(A1:A3)"
End Sub

How do I insert plain values without the formula in Excel VBA?

如何在 Excel VBA 中插入没有公式的普通值?

回答by shahkalpesh

Here is what I think, you are looking for

这是我的想法,你正在寻找

range("a4").Value = Evaluate("SUM(A1:A3)")

回答by El Ronnoco

Are you wanting to the actual result of the SUM? I would try using WorksheetFunction.Sumeg

你想要 的实际结果SUM吗?我会尝试使用WorksheetFunction.Sum例如

Dim MyRange as Range
With Worksheets("SheetName")
  Set MyRange = .Range(.Cells(1, 1), .Cells(3, 1))
  .Cells(4,1) = WorksheetFunction.Sum(MyRange)
End With