如何将求和函数输出到单元格中 [英] How to output the sum function into a cell
问题描述
我正在编写一个代码,它将新员工添加到员工列表中,并更新这些员工下方的所有sum函数.我在该网站上搜索了几天,似乎找不到任何存在相同确切问题的人.
I'm writing a code that will add a new employee to a list of employees and update all of the sum functions below those employees. I've searched for a few days on this site and can't seem to find anyone who's having the same exact problem.
在新行中添加了员工的姓名和时间后,我需要在底部的小计将新行包括在总工时的计算中.
Once the employee's name and hours have been added in the new row, I need the subtotals at the bottom to include the new row in it's calculation of total hours.
我使用for ... next循环向下循环到该行,其中x发生了变化.
I've looped down to the row using a for...next loop, with x changing.
我尽力将其输出到我的工作表中.从逻辑上讲这是正确的,但实际上不起作用.只是让我了解我要做什么.
I got as far as getting this to output into my sheet. It's logically correct, but doesn't actually work. It's just to give an idea of what I'm trying to do.
= SUM(Range(Cells(5,17),Cells(x,17)))
=SUM(Range(Cells(5, 17), Cells( x, 17)))
最后,我只希望在小计单元格中显示这样的内容...= SUM(Q5:Q18)
At the end I just want something like this to show up in the subtotal cell... =SUM(Q5:Q18)
非常感谢您!
推荐答案
尝试以下代码:
Sub sample()
dim x as Integer
For x = 5 To 17
Cells(x, 18) = WorksheetFunction.Sum(Range(Cells(5, 17), Cells(x, 17)))
Next
End Sub
OR
如果要将公式作为字符串发送,可以使用以下内容.
If you want to send formula as a string you can use the below.
Sub sample()
dim x as Integer
For x = 5 To 17
Cells(x, 18) = "=Sum(Q5:Q" & x & ")"
Next
End Sub
这篇关于如何将求和函数输出到单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!