Edit: Added sample of raw data below
I run a claim report every month and copy the data into a tab. All of the data is organized into columns and I've been using a spreadsheet full of SumProduct's and CountIf's to count and organize the data based on different sets of criteria but it just takes too long to process so I'm trying to write a VBA sub to accomplish this more efficiently. One of the columns of data is "Adjuster Home Office". This column is basically a list of offices where each claim originated from. I used AdvancedFilter to extract all of the unique values in this column and copy them to a separate tab in column A. Then, in column C, below each location, I have a list of the claim types or "line items" handled at each office. I have no problem getting this part set up. In column D, I need to be able to display the count of each line item at that designated location. This is where all the Countif's and SumProduct's came into play in my old template I had been using. This is where I hit a snag. I'm trying to use For Each loops to count each line item in column B below the first location, then move to the next location in column A and repeat. Below is the code I've tried:
Private Sub CommandButton23_Click()
Dim linerngs As Range
Dim lineitem As Range
Dim lastlinerow As Long
Dim wsf
Dim TabLastRow
Dim claimstab As String
Dim officesrange As Range
Dim office As Range
claimstab = Sheet2.Range("F2") & " Claims"
TabLastRow = Sheets(claimstab).Cells(Sheets(claimstab).Rows.Count, "A").End(xlUp).Row
Set wsf = Application.WorksheetFunction
officeslastrow = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
lastlinerow = Sheet2.Range("C" & Rows.Count).End(xlUp).Row
Set officerng = Range("A6:A" & officeslastrow).SpecialCells(xlCellTypeConstants, 23)
Set linerngs = Range("C7:C" & lastlinerow).SpecialCells(xlCellTypeConstants, 23)
For Each office In officerng
For Each lineitem In linerngs
If InStr(1, lineitem.Value, "IN") > 0 And InStr(1, lineitem.Value, "AOS") = 0 Then
lineitem.Offset(0, 3) = Application.WorksheetFunction.SumProduct(wsf.CountIfs(Sheets(claimstab).Range("B2:B" & TabLastRow), office))
End If
Next lineitem
Next office
End Sub
I know this is incorrect because these loops are going to loop through everything in column B rather than just the line items below each location. So what I end up with is the count of the last location displayed for every line item in the entire column. below is an example of what I need it to look like. Right now, all im concerned with is setting up the loop to run correctly.
example of what I currently get
[
example of what I'm trying to get
[
You can see from the first example that I'm getting the value "3" for everything. I included a pivot of the locations and their values. you can see that the last location in the pivot, South Portland, has a count of 3.
Any help would be GREATLY appreciated.
example of raw data
[
Objective
[
[
the list of line items is completed created by a userform asking for user imputs
Cells(),Range(),Rows.Count, etc. For example, change your lines toofficeslastrow = Sheet2.Range("A" & Sheet2.Rows.Count).End(xlUp).Row(and same for other variable).