ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Formula req for macro (https://www.excelbanter.com/excel-programming/387036-formula-req-macro.html)

Gautam[_3_]

Formula req for macro
 
I need a macro which calculates the average for each column, tried the
following code, but didn't work out
activesheet.cells(1,1).formulaR1C1="=Average(R[maxrow]C:R[-2]C)"


Barb Reinhardt

Formula req for macro
 
I presume you calculate maxrow somewhere else.


Try this:

activesheet.cells(1,1).formulaR1C1="=Average(R[" & maxrow & "]C:R[-2]C)"

You will run into a problem with this because your formula is trying to go
up 2 rows from activesheet.cells(1,1) and it can't do that.


"Gautam" wrote:

I need a macro which calculates the average for each column, tried the
following code, but didn't work out
activesheet.cells(1,1).formulaR1C1="=Average(R[maxrow]C:R[-2]C)"



Don Guillett

Formula req for macro
 
Assuming your data for each column starts in row 2 and you want the answer
in row 1.
Sub averagecolumns()
For i = 1 To 8 'num columns
Cells(1, i) = Application.Average(Columns(i))
Next i
End Sub


--
Don Guillett
SalesAid Software

"Gautam" wrote in message
ups.com...
I need a macro which calculates the average for each column, tried the
following code, but didn't work out
activesheet.cells(1,1).formulaR1C1="=Average(R[maxrow]C:R[-2]C)"





All times are GMT +1. The time now is 11:27 AM.

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