VBA Use declared variable as part of a formula

Chris123149

New Member
Joined
Jul 2, 2019
Messages
3
Hi All,

I was hoping somebody here could help me out with a particular line of code that i can't seem to get right. I'm looking to insert an "=average" code when a macro is run,, but the column index of each end of the range can vary depending on the active row. The actual code reads:

yrow = ActiveCell.Row


Cells(yrow, 34).Formula = "=average(range(cells(yrow,2).value):cells(yrow, 32).value)"

but keeps giving me an #name ? error in cell 34. What do i need to change to fix this?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi, maybe something like this.

Code:
Cells(YROW, 34).Formula = "=average($B$" & YROW & ":$AF$" & YROW & ")"
 
Upvote 0
Alternatively, since it's the same row, it can be easier to use R1C1 references:

Code:
Cells(yrow, 34).FormulaR1C1 = "=average(RC2:RC32)"
 
Upvote 0

Forum statistics

Threads
1,215,545
Messages
6,125,455
Members
449,228
Latest member
moaz_cma

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