Using VBA Array Formula without R1C1 notation?

hankistan

New Member
Joined
Dec 19, 2012
Messages
6
Hi Everyone.

I am a novice and this is my first post, but this site has solved many issue for me in the past. Anyway, onto my issue.

I have a data file with 3 sections, and I'm doing calculations on the middle section. For the data that I want counted, I've been using code like this:

Code:
With Range("S170").End(xlDown).Offset(3, 0)
.Value = Application.WorksheetFunction.CountIf(Range("S170", Range("S170").End(xlDown)), "Y")
End With

because the number of rows in the middle section is variable. That works great, but now I have a more complicated calculation; namely, an AVERAGEIF array formula. If I record a macro of the formula it of course uses the R1C1 notation.

Code:
    Selection.FormulaArray = _
        "=AVERAGE(IF((R[4]C[-4]:R[19]C[-4]>0),R[4]C[-4]:R[19]C[-4]))"

I assume that there is a way to use the Ranges as I have in the first example, but I'm getting lost in the syntax. Any assistance would be greatly appreciated.

Thanks,
Hank


P.S. I'm running Excel 2010 and Windows 7
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
This works for me, you may need to adjust the ranges etc:
Code:
i = Range("S" & Rows.Count).End(xlUp).row
Range("S" & i + 3).FormulaArray = "=AVERAGE(IF((O4:O" & i & " >0),O4:O" & i & "))"
 
Upvote 0
Thanks Andrew and Jack for your quick replies!

Andrew- the problem with R1C1 as I understand it for my data is that since I have a section of a file that varies in length across days, r1c1 isn't flexible enough for what I"m doing. Of course it could be my novice level that limiting it's use.

Jack - thanks, this looks pretty good. I have 2 issues with it though:
1. The condition in that IF statement that I need to meet is if (O4:O" & i & " ="Y") and that "Y" is not working in the code (I get a compile error expected: end of statement)
2. Because this is that middle section of the data file using xlUp is also problematic; is there a way to re-write this with xlDown?

Thanks again!
Hank
 
Last edited:
Upvote 0
Try:
Code:
i = Range("S170").End(xlDown).row
Range("S" & i + 3).FormulaArray = "=AVERAGE(IF((O4:O" & i & " =""Y""),O4:O" & i & "))"
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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