Sub FillAverage()
Dim ws As Worksheet
Dim lastRow As Long
Dim teamCol As Integer
Dim leadCol As Integer
Dim metricCol As Integer
Dim team As String
Dim lead As String
Dim sum As Double
Dim count As Long
Dim average As Double
' Set the worksheet containing the raw data
Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with your actual sheet name
' Define the column numbers for Team, Lead, and the metric you want to calculate the average for
teamCol = 2 ' Column B (replace with the appropriate column number)
leadCol = 3 ' Column C (replace with the appropriate column number)
metricCol = 12 ' Column L (replace with the appropriate column number)
' Find the last row of data in column L
lastRow = ws.Cells(ws.Rows.Count, metricCol).End(xlUp).Row
' Loop through each row and calculate the average for each unique combination of Team and Lead
For i = 2 To lastRow ' Assuming the data starts in row 2 and headers are in row 1
' Get the Team and Lead values for the current row
team = ws.Cells(i, teamCol).Value
lead = ws.Cells(i, leadCol).Value
' Initialize sum and count variables for each unique combination of Team and Lead
sum = 0
count = 0
' Loop through the data and calculate the sum and count for the current Team and Lead combination
For j = 2 To lastRow
If ws.Cells(j, teamCol).Value = team And ws.Cells(j, leadCol).Value = lead Then
sum = sum + ws.Cells(j, metricCol).Value
count = count + 1
End If
Next j
' Calculate the average for the current Team and Lead combination
If count > 0 Then
average = sum / count
Else
average = 0 ' Handle the case where there are no data points for the combination
End If
' Fill the calculated average in columns E and L for the current row
ws.Cells(i, metricCol - 10).Value = average ' Column E
ws.Cells(i, metricCol).Value = average ' Column L
Next i
End Sub
Bookmarks