开发者

Move excel formulas programmatically

I have an app which exports data to excel sheet. The user can add some form开发者_运维知识库ulas to excel sheet after it has exported the data like he can do =sum(o1:o12) and put the results in o13. The user also has the option to reexport the data to the same sheet. What might happen...that more or less rows would have been exported depending on some conditions. For example.... last time there 12 rows so the user put the formula the row 13 but this time 20 rows might get exported so excel should be able to move the formula from o13 cell to o21 and should have updated the formula from =sum(o1:12) to =sum(o1:20). In other case...only 5 rows might get exported from app so the formula should shift to o6 cell and formula should have been updated to =som(o1:o5).

Is there any way to ask excel to do this programmatically?


LastRow = Range("O65535").End(xlUp).Row will return the number of the last filled cell in the O column.

Next thing to do is:

Range("O" & LastRow+1).Value = "=Sum(O1:O" & LastRow & ")"
Range("O" & LastRow+1).formula = "=Sum(O1:O" & LastRow & ")" will work as well.

Last thing, when you use LastRow = Range("O65535").End(xlUp).Row, be careful if the last cell in the column O is the sum formula.

This should work but don't hesitate to ask questions if it doesn't

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜