hav*_*r24 9 excel vba sum cells
我在vba中求和单元格有问题.我需要使用Cells(a,b):
Range("A1").function="=SUM(Range(Cells(2,1),Cells(3,2)))"
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
CaB*_*ach 32
函数不是范围内的属性/方法.
如果要对值求和,请使用以下命令:
Range("A1").Value = Application.Sum(Range(Cells(2, 1), Cells(3, 2)))
Run Code Online (Sandbox Code Playgroud)
编辑:
如果你想要公式,那么使用如下:
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$2:$B$3).
Run Code Online (Sandbox Code Playgroud)
如果你总是要使用相同的范围地址,那么你可以使用Rory sugested:
Range("A1").Formula ="=Sum(A2:B3)"
Run Code Online (Sandbox Code Playgroud)
Application.Sum在我的经验中通常不能很好地工作(或者至少VBA开发人员环境因为某种原因不喜欢它).
最适合我的功能是 Excel.WorksheetFunction.Sum()
例:
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.
Run Code Online (Sandbox Code Playgroud)
我想要的另一种方法是将函数直接放入单元格中.这可以通过将函数字符串输入单元格值来完成.下面是一个提供与上面相同结果的示例,除了单元格值是函数而不是函数的结果:
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.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
507955 次 |
最近记录: |