Help! Need formula to work right with variable

sagain2k

Board Regular
Joined
Sep 8, 2002
Messages
94
I have a formula that works fine like this:

ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-4]:RC[-1])"

But I now want to be able to use a variable called "calcalnum" to replace the "4" in the above formula. Can't quite get the syntax to work without errors... I'd like to be able to use any function in a formula and use variables to represent the offset numbers.

Getting pointed to the correct syntax for this would be appreciated! Thanks!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Tom Urtis

MrExcel MVP
Joined
Feb 10, 2002
Messages
11,270
Try this. The error trap is for when no value, or a non-numeric value, exists in your range of interest.

Sub Calc()
Dim calcalnum As Range
Set calcalnum = ActiveCell.Offset(0, 4)
On Error Resume Next
ActiveCell.Value = Application.WorksheetFunction.Average(calcalnum, calcalnum.Offset(0, 1))
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,504
Messages
5,832,106
Members
430,111
Latest member
Francis Xavier

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
Top