Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

excel - VBA code to sum a column in a changing Table with the sum above the header

I'm new to VBA. My macro recorder created this code when I added a sum for each month at the top of my table above the Header. When the table grew the sum did not include the new lines at the bottom so I put 800 in there to ensure I have the correct value (my table has 540 rows of data right now). This is a bandaid and I would like to fix the code so that it sums all values in the table for that column. Then I autofilled the formula across all 12 months.

Range("H2").Select
ActiveCell.FormulaR1C1 = "=SUM(R[2]C:R[800]C)"
Range("H2").Select
Selection.AutoFill Destination:=Range("H2:T2"), Type:=xlFillDefault
Range("H2:T2").Select

I found this:

Range("H4", Range("H1048576"),End(xlUp)).Select

which selects the right range for a table that is discontiguous and changes in size, but I don't know how to incorporate the Sum formula with this variable column range. Can someone help me with syntax? Thanks!

question from:https://stackoverflow.com/questions/65836057/vba-code-to-sum-a-column-in-a-changing-table-with-the-sum-above-the-header

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...