Dynamic AVERAGEIF in macro

NoooC

New Member
Joined
Mar 5, 2015
Messages
5
I'm writing a macro and I would like to use an AVERAGEIF with a dynamic selection instead of a fixed selection.

At the moment I use this in my macro:
Code:
AVERAGEIF('sheet1'!R5C11:R10000C11,RC[-1],'sheet1'!R5C12:R10000C12)"

The problem is now, if I have more then 10000 rows, they will not be included in my AVERAGEIF.

I have created a variable to know how many rows 'sheet1' are used:
Code:
Dim usedRangeSheet1 As Integer
    usedRangeSheet1 = Sheets("sheet1").usedRange.Rows.Count

How can I, in my code, change 10000 with usedRangeSheet1?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi,

Does this help?

Code:
AVERAGEIF('sheet1'!R5C11:R10000C11,RC[-1],'sheet1'!R5C12:R" & usedRangeSheet1 & "C12)"

I'd be tempted to name the last row variable as something more appropriate, and you could use this to define it..

Code:
Dim iLastRow as Integer
iLastRow = sheets("Sheet1").cells(Sheets("Sheet1").rows.count,12).end(xlup).row
 
Upvote 0

Forum statistics

Threads
1,215,885
Messages
6,127,571
Members
449,385
Latest member
KMGLarson

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