Averaging values in a column with conditions in two other columns

DPECK

New Member
Joined
Sep 4, 2014
Messages
31
I have a 57 column, 8832 row spread sheet that I'm extracting data from and creating tables and charts. I'm wanting to calculate the average number of days it took to complete each work order. I have a column with the "days to complete" (col AE) and the conditions that I want to meet are status (col H) = Comp and CrewId (this will change with the crew. Elect, I&C, FireSys... )(col AH). How do I write the VBA code for this?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Welcome to the board.

Do you need to use VBA? If not, you can easily achieve this using the AVERAGEIFS function.
 
Upvote 0
Welcome to the board.

Do you need to use VBA? If not, you can easily achieve this using the AVERAGEIFS function.


My preference is to use VBA because I'm automating the report generation, data sorting and chart creation. As well as posting the charts to an intranet web page
<o:p></o:p>
 
Upvote 0
Take a look at this example. It will place the average of Comps for Crew 1 for rows 2 through 10 in cell A1:
Code:
Sub AveIfs()

    Range("A1") = WorksheetFunction.AverageIfs(Range("AE2:AE10"), Range("H2:H10"), _
        "Comp", Range("AH2:AH10"), "Crew 1")
    
End Sub
 
Upvote 0
Thank you for your response. Here is a sample of the data I'm working with. On another worksheet with in this workbook in a column and the respective row for each crew is where I want the information to go. Averaging "Days To Complete" based on the status of "Comp" excluding work types "PM" and "PdM" for each crew.

WONUMWORKTYPESTATUSTARGCOMPDATEACTFINISHDAYS TO COMPLETEREPORTDATECREWIDACTLABHRSDays from DueDue Statusdays from reported
11030162CMCOMP2014-08-31 01:14:012014-08-04 07:20:343.32014-08-01 01:13:35FAC GENERAL MAINT2.04overdue33
11032961CMCOMP2014-08-15 09:56:012014-08-07 07:35:295.92014-08-01 09:55:20FAC GENERAL MAINT2.019overdue33
11026712CMWMATL2014-09-24 04:31:49 -41849.22014-07-29 04:30:59FAC GENERAL MAINT3.0-2035
11044547CMAPPR2014-09-06 15:11:20 -41858.62014-08-07 15:11:20FAC GENERAL MAINT3.3-2due in 5 days27
11026639PMACCEPT2014-09-29 04:00:00 -41849.12014-07-29 02:11:25FAC GENERAL MAINT0.0-2535
11028572PMACCEPT2014-09-29 04:00:00 -41850.12014-07-30 02:16:14FAC GENERAL MAINT0.0-2534
10336110CMCOMP2014-07-31 06:42:172014-07-21 14:46:46356.32013-07-30 06:42:17FAC PROCESS MAINT0.034overdue394
10868927CMCOMP2014-06-04 16:22:502014-05-06 15:38:591.02014-05-05 16:22:50FAC PROCESS MAINT0.090overdue119
10870549CMCOMP2014-05-16 12:27:252014-05-12 06:46:135.72014-05-06 13:26:07FAC PROCESS MAINT0.0108overdue118
10882131CMCOMP2014-06-27 13:50:202014-06-02 15:10:4819.12014-05-14 13:50:20FAC PROCESS MAINT0.067overdue110
11073252CMINPRG2014-09-12 10:41:25 -41876.42014-08-25 10:41:25FAC PROCESS MAINT0.0-89
11074872CMINPRG2014-09-02 11:42:34 -41877.52014-08-26 11:42:34FAC PROCESS MAINT0.02overdue8
11033092CMOPEN2014-09-26 15:54:20 -41852.72014-08-01 15:54:20FAC PROCESS MAINT0.0-2233
11036568PMACCEPT2014-09-02 04:00:00 -41854.12014-08-03 02:03:25FAC PROCESS MAINT0.02overdue31
11037080PMACCEPT2014-09-03 03:30:00 -41855.12014-08-04 02:01:29FAC PROCESS MAINT0.01overdue30
11041451PMWMATL2014-08-31 10:00:00 -41856.52014-08-05 12:46:05FAC PROCESS MAINT0.04overdue29
11056773PMWMATL2014-09-14 15:24:11 -41866.62014-08-15 15:24:11FAC PROCESS MAINT1.0-1019
10913184PMINPRG2014-08-31 10:00:00 -41791.12014-06-01 02:01:43FAC PROCESS MAINT7.54overdue93
10857223PMCOMP2014-05-31 10:00:002014-05-26 14:35:5326.52014-04-30 02:04:20FAC PROCESS MAINT8.094overdue124
10857246PMCOMP2014-05-31 10:00:002014-05-26 14:36:1926.52014-04-30 02:04:38FAC PROCESS MAINT8.094overdue124
11046891PMINPRG2014-08-29 10:00:00 -41859.42014-08-08 10:03:52FAC PROCESS MAINT12.05overdue26
10837343PMCOMP2014-05-18 10:00:002014-05-15 15:17:3627.52014-04-18 02:07:30FAC PROCESS MAINT23.5106overdue136
11046871PMINPRG2014-08-29 02:00:00 -41859.42014-08-08 10:00:27FAC PROCESS MAINT26.05overdue26
11047015RMACCEPT2014-09-07 14:49:31 -41859.62014-08-08 14:49:31FAC PROCESS MAINT0.0-3due in 5 days26
11055510RMACCEPT2014-09-13 19:37:05 -41865.82014-08-14 19:37:05FAC PROCESS MAINT0.0-920
10254370RMCOMP2014-09-19 15:33:332014-08-21 07:26:53424.12013-06-23 04:35:50FAC PROCESS MAINT0.0-15431
11033063RMCOMP2014-08-08 15:19:592014-08-08 15:10:567.02014-08-01 15:19:59FAC PROCESS MAINT0.026overdue33
10457657CPCOMP 2014-02-26 16:42:30153.02013-09-26 17:43:07FIRE PROTECTION19.00#N/A338
9839413CPCOMP2011-07-13 09:19:492013-07-25 11:45:40239.02012-11-28 10:53:44FIRE PROTECTION148.51131overdue636
11056935PMACCEPT2014-09-15 03:00:00 -41867.12014-08-16 02:02:39FIRE PROTECTION0.0-1118
11066453PMACCEPT2014-08-29 02:30:00 -41874.12014-08-23 02:07:50FIRE PROTECTION0.05overdue11
11056617CMACCEPT2014-09-14 08:35:15 -41866.42014-08-15 08:35:15FAC ELECTRICAL0.0-1019
11076920CMACCEPT2014-09-26 09:31:28 -41878.42014-08-27 09:31:28FAC ELECTRICAL0.0-227
10967280CMAPPR2014-08-31 15:02:00 -41815.62014-06-25 15:02:36FAC ELECTRICAL0.04overdue69
10481786CMCOMP2014-05-31 10:07:362014-06-12 11:55:10246.92013-10-08 15:19:32FAC ELECTRICAL0.094overdue326
10828770CMCOMP2014-05-12 00:11:022014-05-09 10:42:5527.42014-04-12 00:11:02FAC ELECTRICAL0.0112overdue142
10839906CMCOMP 2014-04-26 16:07:007.82014-04-18 20:41:34FAC ELECTRICAL0.00#N/A136
10757622PMPENSCHED2014-09-17 11:40:00 -41701.42014-03-03 10:40:51FAC ELECTRICAL0.0-13181
9987019PMPENSCHED2014-09-12 06:00:00 -41315.12013-02-10 02:02:23FAC ELECTRICAL0.0-8564
10813853PMWMATL2014-07-15 04:00:00 -41732.12014-04-03 02:05:47FAC ELECTRICAL0.049overdue151
10821140PMWMATL2014-07-15 04:00:00 -41737.12014-04-08 02:07:51FAC ELECTRICAL0.049overdue146
11053961PMWSCH2014-08-28 04:00:00 -41865.12014-08-14 02:12:16FAC ELECTRICAL0.06overdue20
10602175PMCOMP2014-09-30 06:00:002014-05-30 12:34:52168.42013-12-13 02:09:56FAC ELECTRICAL1.0-26261
11004659PMPENSCHED2014-07-16 04:00:00 -41836.12014-07-16 02:16:22FAC ELECTRICAL1.048overdue48
9858409PMINPRG2014-07-05 06:00:00 -41250.12012-12-07 02:08:05FAC ELECTRICAL2.359overdue627
10215179PMINPRG2014-01-31 10:00:00 -41427.12013-06-02 02:10:53FAC ELECTRICAL2.5214overdue452

<COLGROUP><COL style="WIDTH: 48pt" width=64><COL style="WIDTH: 55pt; mso-width-source: userset; mso-width-alt: 2669" width=73><COL style="WIDTH: 45pt; mso-width-source: userset; mso-width-alt: 2194" width=60><COL style="WIDTH: 98pt; mso-width-source: userset; mso-width-alt: 4754" width=130><COL style="WIDTH: 100pt; mso-width-source: userset; mso-width-alt: 4864" width=133><COL style="WIDTH: 53pt; mso-width-source: userset; mso-width-alt: 2560" width=70><COL style="WIDTH: 82pt; mso-width-source: userset; mso-width-alt: 3986" width=109><COL style="WIDTH: 86pt; mso-width-source: userset; mso-width-alt: 4169" width=114><COL style="WIDTH: 58pt; mso-width-source: userset; mso-width-alt: 2816" width=77><COL style="WIDTH: 71pt; mso-width-source: userset; mso-width-alt: 3437" width=94><COL style="WIDTH: 64pt; mso-width-source: userset; mso-width-alt: 3108" width=85><COL style="WIDTH: 87pt; mso-width-source: userset; mso-width-alt: 4242" width=116><TBODY>
</TBODY>
 
Upvote 0
I copied your sample table into sheet 1 A1:L47. Here are the results of the averages:

FAC GENERAL MAINT4.6
FAC PROCESS MAINT135.5333333
FIRE PROTECTION196
FAC ELECTRICAL94.03333333

<colgroup><col><col></colgroup><tbody>
</tbody>

Here is the code I used. It will need to be adjusted to your actual data:

Code:
Sub CrewAve()

    Dim x As Long
    Application.ScreenUpdating = False
    
    Sheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Crew Averages"
    Range("A1") = "Crew ID"
    Range("B1") = "Average Days"
    
    For x = 2 To Sheets(1).Range("H" & Rows.Count).End(xlUp).Row
        If Sheets(1).Range("H" & x) <> Sheets(1).Range("H" & x).Offset(-1, 0) Then
            Range("A" & Range("A" & Rows.Count).End(xlUp).Row).Offset(1, 0) = Sheets(1).Range("H" & x)
        End If
    Next x
    
    For x = 2 To Range("A" & Rows.Count).End(xlUp).Row
        Range("B" & x) = WorksheetFunction.AverageIfs(Sheets(1).Range("F:F"), _
        Sheets(1).Range("H:H"), Range("A" & x), _
        Sheets(1).Range("C:C"), "Comp", _
        Sheets(1).Range("B:B"), "<>PM", _
        Sheets(1).Range("B:B"), "<>PdM")
    Next x
    
    Columns("A:B").AutoFit
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
The code below works through the first two loops then I get a Run-time error '13': Type mismatch (shown red text). If I remove the DueStatus portion of the two loops that it errors in the remaining 6 loops deliver the desired data and the two modified loops follow the remaining code. I'm lost on this problem.

Sub OVERDUE_DWO()


Dim x As Integer
Dim area As String
Dim PastDue As Long
Dim i As Long
Dim status As String
Dim workType As String
Dim DueStatus As String


For x = 1 To 9
area = Sheets("DWOs").Cells(x, 1).Value
PastDue = 0
If area = "FAC PROCESS MAINT" Then
i = 1
Do Until IsEmpty(Sheets("DATA").Cells(i, 34))
If Sheets("DATA").Cells(i, 34).Value = area Then
status = Sheets("DATA").Cells(i, 8).Value
workType = Sheets("DATA").Cells(i, 7).Value
If Not status = "COMP" And Not status = "HOLD" And Not status = "" And Not workType = "PM" _
And Not workType = "PDM" And Not workType = "CP" And Not workType = "" Then
DueStatus = Sheets("DATA").Cells(i, 56).Value
If DueStatus = "Overdue" Then
PastDue = PastDue + 1
End If
End If
End If
i = i + 1
Loop
Sheets("DWOs").Cells(x, 3).Value = PastDue

ElseIf area = "FAC GENERAL MAINT" Then
i = 1
Do Until IsEmpty(Sheets("DATA").Cells(i, 34))
If Sheets("DATA").Cells(i, 34).Value = area Then
status = Sheets("DATA").Cells(i, 8).Value
workType = Sheets("DATA").Cells(i, 7).Value
If Not status = "COMP" And Not status = "HOLD" And Not status = "" And Not workType = "PM" _
And Not workType = "PDM" And Not workType = "CP" And Not workType = "" Then
DueStatus = Sheets("DATA").Cells(i, 56).Value
If DueStatus = "Overdue" Then
PastDue = PastDue + 1
End If
End If
End If
i = i + 1
Loop
Sheets("DWOs").Cells(x, 3).Value = PastDue

ElseIf area = "FAC ELECTRICAL" Then
i = 1
Do Until IsEmpty(Sheets("DATA").Cells(i, 34))
If Sheets("DATA").Cells(i, 34).Value = area Then
status = Sheets("DATA").Cells(i, 8).Value
workType = Sheets("DATA").Cells(i, 7).Value
If Not status = "COMP" And Not status = "HOLD" And Not status = "" And Not workType = "PM" _
And Not workType = "PDM" And Not workType = "CP" And Not workType = "" Then
DueStatus = Sheets("DATA").Cells(i, 56).Value
If DueStatus = "Overdue" Then
PastDue = PastDue + 1
End If
End If
End If
i = i + 1
Loop
Sheets("DWOs").Cells(x, 3).Value = PastDue

ElseIf area = "FAC FIRESYS" Then
i = 1
Do Until IsEmpty(Sheets("DATA").Cells(i, 34))
If Sheets("DATA").Cells(i, 34).Value = area Then
status = Sheets("DATA").Cells(i, 8).Value
workType = Sheets("DATA").Cells(i, 7).Value
If Not status = "COMP" And Not status = "HOLD" And Not status = "" And Not workType = "PM" _
And Not workType = "PDM" And Not workType = "CP" And Not workType = "" Then
DueStatus = Sheets("DATA").Cells(i, 56).Value
If DueStatus = "Overdue" Then
PastDue = PastDue + 1
End If
End If
End If
i = i + 1
Loop
Sheets("DWOs").Cells(x, 3).Value = PastDue

ElseIf area = "FAC FMCS" Then
i = 1
Do Until IsEmpty(Sheets("DATA").Cells(i, 34))
If Sheets("DATA").Cells(i, 34).Value = area Then
status = Sheets("DATA").Cells(i, 8).Value
workType = Sheets("DATA").Cells(i, 7).Value
If Not status = "COMP" And Not status = "HOLD" And Not status = "" And Not workType = "PM" _
And Not workType = "PDM" And Not workType = "CP" And Not workType = "" Then
DueStatus = Sheets("DATA").Cells(i, 56).Value
If DueStatus = "Overdue" Then
PastDue = PastDue + 1
End If
End If
End If
i = i + 1
Loop
Sheets("DWOs").Cells(x, 3).Value = PastDue

ElseIf area = "FAC BALANCE" Then
i = 1
Do Until IsEmpty(Sheets("DATA").Cells(i, 34))
If Sheets("DATA").Cells(i, 34).Value = area Then
status = Sheets("DATA").Cells(i, 8).Value
workType = Sheets("DATA").Cells(i, 7).Value
If Not status = "COMP" And Not status = "HOLD" And Not status = "" And Not workType = "PM" _
And Not workType = "PDM" And Not workType = "CP" And Not workType = "" Then
DueStatus = Sheets("DATA").Cells(i, 56).Value
If DueStatus = "Overdue" Then
PastDue = PastDue + 1
End If
End If
End If
i = i + 1
Loop
Sheets("DWOs").Cells(x, 3).Value = PastDue

ElseIf area = "FAC PREDICTIVE" Then
i = 1
Do Until IsEmpty(Sheets("DATA").Cells(i, 34))
If Sheets("DATA").Cells(i, 34).Value = area Then
status = Sheets("DATA").Cells(i, 8).Value
workType = Sheets("DATA").Cells(i, 7).Value
If Not status = "COMP" And Not status = "HOLD" And Not status = "" And Not workType = "PM" _
And Not workType = "PDM" And Not workType = "CP" And Not workType = "" Then
DueStatus = Sheets("DATA").Cells(i, 56).Value
If DueStatus = "Overdue" Then
PastDue = PastDue + 1
End If
End If
End If
i = i + 1
Loop
Sheets("DWOs").Cells(x, 3).Value = PastDue

ElseIf area = "FIRE PROTECTION" Then
i = 1
Do Until IsEmpty(Sheets("DATA").Cells(i, 34))
If Sheets("DATA").Cells(i, 34).Value = area Then
status = Sheets("DATA").Cells(i, 8).Value
workType = Sheets("DATA").Cells(i, 7).Value
If Not status = "COMP" And Not status = "HOLD" And Not status = "" And Not workType = "PM" _
And Not workType = "PDM" And Not workType = "CP" And Not workType = "" Then
DueStatus = Sheets("DATA").Cells(i, 56).Value
If DueStatus = "Overdue" Then
PastDue = PastDue + 1
End If
End If
End If
i = i + 1
Loop
Sheets("DWOs").Cells(x, 3).Value = PastDue

End If
Next x
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,453
Members
448,898
Latest member
drewmorgan128

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