AverageIf

hansgrandia

Board Regular
Joined
Jan 10, 2015
Messages
53
Hello,

If I run below standing code in Sheets("Blad2"), it only displays the 4, 35 and 44 without doing the calculation. The code goes to "end sub" the moment is has touched For y = 2 To x. If I run the code in Sheets("Blad1") everything runs als expectated. What should I do to have the average of the customer regardless in which sheet I press the button?

Thank you. Regards,
Hans Grandia
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


x = Cells(Rows.Count, 1).End(xlUp).Row


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 Result.Cells(y, "A") <> "" Then
Result.Cells(y, 2).Value = WorksheetFunction.AverageIf(Customers, Result.Range("A" & y), GemLT)
End If
Next y


End Sub
 

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.
What is the value of x when you run the code and it jumps to End Sub at the line:

For y = 2 to x

??
 
Upvote 0
Great answer. Thanks for not giving me the answer immediately. This is learnfull. I brought down the piece of code in the script and everything runs perfectly.
Reegards,
Hans Grandia
Netherlands
 
Upvote 0

Forum statistics

Threads
1,215,982
Messages
6,128,104
Members
449,421
Latest member
AussieHobbo

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