Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AVERAGE function

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
I'm writing some code in which i have a row number stored in an integer variable. I'm trying to do the following and its not working so wondered if anyone has an alternative:
Code:
  'rowNum has already been set by previous code and is a valid integer
  ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-2]:R[rowNum]C[-2])"
Clive [infinity]
 
Stretchwickster

Use the technique

Dim MyFormula as string

MyFormula = "=AVERAGE(RC[-2]:R[" & rowNum & "]C[-2])"
ActiveCell.FormulaR1C1 = MyFormula

Using your version, the worksheet function was trying to use the variable rowNum and so an error occurred.

For functions like this, remember that VBA knows what the value of the variable is, so use VBA to create a function using that variable VALUE.

Hope this helps

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top