give this a try
Option Explicit
Sub aaa()
Const shSurvey As String = "Site Survey"
Const shData As String = "Site Data"
Dim AllCells As Range, Cell As Range
Dim UniqueValues As New Collection
Dim i As Integer, j As Integer
Dim Swap1, Swap2, Item
Dim Lastrow As Long
Dim a As Variant
Worksheets(shSurvey).AutoFilterMode = False
' Find lastrow on Column A
Lastrow = Worksheets(shSurvey).Cells(Rows.CountLarge, 1).End(xlUp).Row
' Get items are in Column A
Set AllCells = Worksheets(shSurvey).Range("A3:A" & Lastrow)
' The next statement ignores the error caused
' by attempting to add a duplicate key to the collection.
' The duplicate is not added - which is just what we want!
On Error Resume Next
For Each Cell In AllCells
UniqueValues.Add Cell.Value, CStr(Cell.Value)
' Note: the 2nd argument (key) for the Add method must be a string
Next Cell
' Resume normal error handling
On Error GoTo 0
' Sort the collection (optional)
For i = 1 To UniqueValues.Count - 1
For j = i + 1 To UniqueValues.Count
If UniqueValues(i) > UniqueValues(j) Then
Swap1 = UniqueValues(i)
Swap2 = UniqueValues(j)
UniqueValues.Add Swap1, before:=j
UniqueValues.Add Swap2, before:=i
UniqueValues.Remove i + 1
UniqueValues.Remove j + 1
End If
Next j
Next i
i = 1
ReDim a(1 To UniqueValues.Count, 1 To 6)
' Now due something with the sorted non-duplicated items
Application.ScreenUpdating = False
For Each Item In UniqueValues
With Worksheets(shSurvey)
.Range("A2:A" & Lastrow).AutoFilter Field:=1, Criteria1:=Item
Set AllCells = .Range("A3:G" & Lastrow).SpecialCells(xlCellTypeVisible)
a(i, 1) = .Cells(AllCells.Row, 1)
a(i, 2) = .Cells(AllCells.Row, 2)
a(i, 3) = Evaluate("=SUBTOTAL(9,'" & shSurvey & "'" & "!D3:D" & Lastrow & ")")
a(i, 4) = Evaluate("=SUBTOTAL(9,'" & shSurvey & "'" & "!E3:E" & Lastrow & ")")
a(i, 5) = Evaluate("=SUBTOTAL(9,'" & shSurvey & "'" & "!F3:F" & Lastrow & ")")
a(i, 6) = Evaluate("=SUBTOTAL(9,'" & shSurvey & "'" & "!G3:G" & Lastrow & ")")
End With
i = i + 1
Next Item
Worksheets(shData).Range("a3").Resize(UBound(a), UBound(a, 2)) = a
Worksheets(shSurvey).AutoFilterMode = False
Application.ScreenUpdating = False
Erase a
Set AllCells = Nothing
Set Cell = Nothing
End Sub
Bookmarks