A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
First the explanation of why we were seeing double-entry of one of the department codes: Turns out that in using the Advanced Filtering to get unique list of entries, it wants to use the first entry as a label for the resulting created unique list. I've changed the code to take that into account. Notice that I changed from using "FirstDataRow = 2" to "DeptLabelRow = 1" up in the declarations. That causes a couple of downstream changes to put the formulas into the correct rows (not putting one next to the new "Dept" label in the unique list).
I also added some code to clear the contents of all rows below the end of the 'source list' - that's in case you delete some entries within it or modify it such that it doesn't have as many departments listed in it as were there when a unique list was created previously - we might only write the remaining department information, but there could be old entries below that remaining from now unused departments.
The revised code:
Sub MakeSummaryCount()
'change these to match your worksheet layout
Const NumberCol = "A"
Const DeptCol = "B"
Const DeptLabelRow = 1
'working variables
Dim lastListRow As Long
Dim firstNewRow As Long
Dim deptList As Range
Dim deptListColNumber As Integer
Dim lastUsedRow As Long
lastUsedRow = ActiveSheet.UsedRange.Rows.Count
lastListRow = Range(NumberCol & DeptLabelRow).End(xlDown).Row
firstNewRow = lastListRow + 2
'clean up any old summary information below the original list
'if you have anything below the last row in the list of departments
'in any column of the workbook, don't include the next 3 lines
If lastUsedRow > lastListRow Then
Rows(lastListRow + 1 & ":" & lastUsedRow).ClearContents
End If
Range("B" & firstNewRow) = "ISSUED"
'create unique list
Set deptList = Range(DeptCol & DeptLabelRow & ":" & _
DeptCol & lastListRow)
'use advanced filtering to create unique list of Departments
'NOTE: Advanced Filtering used in this way uses the 1st entry
'in the source list as a label - which can end up making it
'appear as if it has entered one data field twice if you don't
'actually have a label in the first cell of the source list - JLL
deptList.AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Range("A" & firstNewRow), Unique:=True
'get address of the new unique list entries
deptListColNumber = deptList.Column
'reuse deptList for this
Set deptList = Range("A" & firstNewRow + 1 & ":" & _
Range("A" & firstNewRow).End(xlDown).Address)
'create a COUNTIF() formula for column B in the summary area
deptList.Offset(0, 1).FormulaR1C1 = _
"=COUNTIF(R" & DeptLabelRow + 1 & "C" & deptListColNumber & _
":R" & lastListRow & "C" & deptListColNumber & _
",RC[-1])"
'this converts the formulas to values
deptList.Offset(0, 1).Formula = deptList.Offset(0, 1).Value
Set deptList = Nothing
End Sub