ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Macro code to autosum a dynamic length column (https://www.excelbanter.com/excel-discussion-misc-queries/80877-macro-code-autosum-dynamic-length-column.html)

SlowPoke

Macro code to autosum a dynamic length column
 
I have a macro where I want to get some totals of a couple columns. I
have the row number in a variable where I will put the totals, but
cannot figure out how to do the autosum. Here is the code I am trying
to get working. nRow is the row for the totals. I want to total
columns M and N from row 2 to nRow. I would appreciate some help in
this.

nRow = nRow + 2
Cells(nRow, 1) = "Totals"
' Range(nRow).Select
' ActiveCell.FormulaR1C1 = "Totals"
Range("M2:M" & nRow).Select
' cells(nRow,13)
ActiveCell.FormulaR1C1 =
"=SUM(M2:M7,(IF(COUNT(M9:M990)0,OFFSET(M9,0,0,COU NT(M9:M990)),0)))"

' ActiveCell.FormulaR1C1 = "=SUM(M2:MR[-1]C)"
' Range("N2:N26").Select
' Range("N26").Activate
' ActiveCell.FormulaR1C1 = "=SUM(R[-24]C:R[-1]C)"


Bob Phillips

Macro code to autosum a dynamic length column
 
How about

Cells(nRow,"M").Formula = "=SUM("M2:M" & nRow - 1 & ")"

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"SlowPoke" wrote in message
ups.com...
I have a macro where I want to get some totals of a couple columns. I
have the row number in a variable where I will put the totals, but
cannot figure out how to do the autosum. Here is the code I am trying
to get working. nRow is the row for the totals. I want to total
columns M and N from row 2 to nRow. I would appreciate some help in
this.

nRow = nRow + 2
Cells(nRow, 1) = "Totals"
' Range(nRow).Select
' ActiveCell.FormulaR1C1 = "Totals"
Range("M2:M" & nRow).Select
' cells(nRow,13)
ActiveCell.FormulaR1C1 =
"=SUM(M2:M7,(IF(COUNT(M9:M990)0,OFFSET(M9,0,0,COU NT(M9:M990)),0)))"

' ActiveCell.FormulaR1C1 = "=SUM(M2:MR[-1]C)"
' Range("N2:N26").Select
' Range("N26").Activate
' ActiveCell.FormulaR1C1 = "=SUM(R[-24]C:R[-1]C)"




SlowPoke

Macro code to autosum a dynamic length column
 
It gives a compile error on compile pointing to M2 "Expected end of
statement"


Bob Phillips

Macro code to autosum a dynamic length column
 
Sorry, my typo

Cells(nRow,"M").Formula = "=SUM(M2:M" & nRow - 1 & ")"

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"SlowPoke" wrote in message
oups.com...
It gives a compile error on compile pointing to M2 "Expected end of
statement"





All times are GMT +1. The time now is 09:44 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com