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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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
 
Upvote 0

Forum statistics

Threads
1,215,580
Messages
6,125,654
Members
449,245
Latest member
PatrickL

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