VBA: AverageIf

hansgrandia

Board Regular
Joined
Jan 10, 2015
Messages
53
Hello,

I have worked out below standing code, but it's ignoring the AverageIf part of it. What did I do wrong here? The cells (average) remain empty without error message.

Thank you for your help!

Regards,
Hans
Netherlands

.....................

Sub ReturnLeadTime()


Dim Data As Worksheet
Dim Result As Worksheet
Dim GemLT As Range
Dim Customers As Range
Dim y As Long
Dim x As Long


Set Data = Sheets("Blad1")
Set Result = Sheets("Blad2")
Set Customers = Worksheets("Blad1").Range("A2:A51")
Set GemLT = Worksheets("Blad1").Range("E2:E51")


'Subcategories in Column A
Result.Range("A1").Value = "Customer"
Result.Range("B1").Value = "Average"
Result.Range("A2").Value = "4"
Result.Range("A3").Value = "35"
Result.Range("A4").Value = "44"


'Per subcategorie totaal berekenen


For y = 2 To x


If Cells(y, "A") <> "" Then
Cells(y, 2).Value = WorksheetFunction.AverageIf(Customers, Range("A" & y), GemLT)
End If
Next y


End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Perhaps you need to qualify the sheet in that loop code..
I would guess it should be the Result Sheet

Code:
If Result.Cells(y, "A") <> "" Then
    Result.Cells(y, 2).Value = WorksheetFunction.AverageIf(Customers, Result.Range("A" & y), GemLT)
End If
Next y
 
Upvote 0
I think you have ommitted to include a value for "x". You define the variable, but there is no actual value given to it.
 
Upvote 0

Forum statistics

Threads
1,216,014
Messages
6,128,287
Members
449,436
Latest member
blaineSpartan

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