vba VBA中的求和函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11707888/
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
Sum function in VBA
提问by haver24
I have a problem with summing cells in vba. I need to use Cells(a,b):
我在 vba 中对单元格求和时遇到问题。我需要使用单元格(a,b):
Range("A1").function="=SUM(Range(Cells(2,1),Cells(3,2)))"
but it doesn't work.
但它不起作用。
回答by CaBieberach
Function is not a property/method from range.
函数不是范围内的属性/方法。
If you want to sum values then use the following:
如果要对值求和,请使用以下命令:
Range("A1").Value = Application.Sum(Range(Cells(2, 1), Cells(3, 2)))
EDIT:
编辑:
if you want the formula then use as follows:
如果你想要这个公式,那么使用如下:
Range("A1").Formula = "=SUM(" & Range(Cells(2, 1), Cells(3, 2)).Address(False, False) & ")"
'The two false after Adress is to define the address as relative (A2:B3).
'If you omit the parenthesis clause or write True instead, you can set the address
'as absolute ($A:$B).
In case you are allways going to use the same range address then you can use as Rory sugested:
如果您总是要使用相同的范围地址,那么您可以使用 Rory sugested:
Range("A1").Formula ="=Sum(A2:B3)"
回答by Lopsided
Place the function valueinto the cell
将函数值放入单元格中
Application.Sum often does not work well in my experience (or at least the VBA developer environment does not like it for whatever reason).
根据我的经验,Application.Sum 通常不能很好地工作(或者至少 VBA 开发人员环境无论出于何种原因都不喜欢它)。
The function that works best for me is Excel.WorksheetFunction.Sum()
最适合我的功能是 Excel.WorksheetFunction.Sum()
Example:
例子:
Dim Report As Worksheet 'Set up your new worksheet variable.
Set Report = Excel.ActiveSheet 'Assign the active sheet to the variable.
Report.Cells(11, 1).Value = Excel.WorksheetFunction.Sum(Report.Range("A1:A10")) 'Add the function result.
Place the function directly into the cell
将函数直接放入单元格中
The other method which you were looking for I think is to place the function directly into the cell. This can be done by inputting the function string into the cell value. Here is an example that provides the same result as above, except the cell value is given the function and not the result of the function:
我认为您正在寻找的另一种方法是将函数直接放入单元格中。这可以通过将函数字符串输入到单元格值中来完成。这是一个提供与上述相同结果的示例,除了单元格值被赋予函数而不是函数的结果:
Dim Report As Worksheet 'Set up your new worksheet variable.
Set Report = Excel.ActiveSheet 'Assign the active sheet to the variable.
Report.Cells(11, 1).Value = "=Sum(A1:A10)" 'Add the function.
回答by Tim Williams
Range("A1").Function="=SUM(Range(Cells(2,1),Cells(3,2)))"
won't work because worksheet functions (when actually used on a worksheet) don't understand Range
or Cell
将不起作用,因为工作表函数(在工作表上实际使用时)不理解Range
或Cell
Try
尝试
Range("A1").Formula="=SUM(" & Range(Cells(2,1),Cells(3,2)).Address(False,False) & ")"
回答by Mohamed Khairt
Range("A10") = WorksheetFunction.Sum(Worksheets("Sheet1").Range("A1", "A9"))
Where
在哪里
Range("A10")
is the answer cell
Range("A10")
是答案单元格
Range("A1", "A9")
is the range to calculate
Range("A1", "A9")
是要计算的范围