Difficulty with relative addressing (averaging a column)

looknsee

New Member
Joined
Apr 23, 2015
Messages
9
I'm trying to write a Visual Basic program. In a spreadsheet, I have a column of data, and I want to define a cell under the column that contains the average of the cells in the column. The trouble is that I don't know the exact position or length of the column.

I have no trouble selecting the cell under the column, using a construct like...
Cells( MyRow + 1, MyCol )
(where "MyRow" is the row number of the cell at the bottom of the column.

I thought to record a macro where I perform the actions for a specific column, and then I would generalize the resulting code to use my variables instead of any kind of hard coding. The line of code that is recorded is this...
ActiveCell.FormulaR1C1 = "=AVERAGE(R[-41]C:R[-1]C)"

But when I try to replace the "-41" with a variable, as in...
ActiveCell.FormulaR1C1 = "=AVERAGE(R[-(MyRow-1)]C:R[-1]C)
I get an error. (I don't know anything about R1C1 style addressing -- pointers appreciated).

I can use "Cells" and/or "Range" to select the appropriate column of data. How can I put the "AVERAGE" formual in Cells( MyRow+1, MyCol ) while specifying the variable length of the column?

Thanks in advance.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
What's the formula supposed to look like and where would you put it?

If the data is in Range(A1:A20), then I want to put "AVERAGE(A1:A20)" into cell A21. My challenge is that the column is not always 20 rows tall; it can be a variable number of rows tall.
 
Upvote 0
You're welcome..

You're entering a formula into a cell.
A Cell formula doesn't know what MyRow is.
It would literally end up looking like
AVERAGE(A1:AMyRow)
Which makes no sense, so it would be an error.

Basically you want to concatenate the value of MyRow into the formula.
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,576
Members
449,173
Latest member
Kon123

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top