A bit more complex than we both anticipated lol....Try this...
Option Explicit
Sub popcorn7()
Dim val, val1, ws As Worksheet
Dim i As Long, ii As Long, sr As Long, lr As Long, numrows As Long, lrow As Long
Application.ScreenUpdating = False
Set ws = Sheets("Daily Input")
With ws
lrow = .Cells(Rows.Count, "W").End(xlUp).Row
Range("A2:AD" & lrow).Sort key1:=Range("I2:I" & lrow), order1:=xlAscending, Header:=xlNo
With .Cells(1).CurrentRegion: .Range(.Cells(2, 9), .Cells(.Count, 9)).AdvancedFilter xlFilterCopy, , .Range("AH1"), True: End With
With .Range("AH1").CurrentRegion: val = .Value: .Clear: End With
For i = 2 To UBound(val)
.Cells(1).CurrentRegion.AutoFilter 9, val(i, 1)
sr = .Range("I2", ws.Range("I" & Rows.Count).End(xlUp)).SpecialCells(12).Row
lr = .Range("I" & Rows.Count).End(xlUp).Row
With .Cells(1).CurrentRegion: .Range(.Cells(sr, 23), .Cells(lr, 23)).SpecialCells(12).AdvancedFilter xlFilterCopy, , .Range("AJ1"), True: End With
With .Range("AJ1").CurrentRegion: val1 = .Value: .Clear: End With
.Cells(1).CurrentRegion.AutoFilter 9, val(i, 1)
For ii = 2 To UBound(val1)
.Cells(1).CurrentRegion.AutoFilter 23, val1(ii, 1)
numrows = .AutoFilter.Range.Columns(23).SpecialCells(12).Cells.Count - 1
If numrows > 1 Then
sr = .Range("I2", ws.Range("I" & Rows.Count).End(xlUp)).SpecialCells(12).Row
lr = .Range("I" & Rows.Count).End(xlUp).Row
.Range("K" & sr) = Application.Subtotal(9, Range("K:K"))
.Range("K" & sr + 1 & ":K" & lr).EntireRow.Delete
End If
.Cells(1).CurrentRegion.AutoFilter 23
Next ii
Next i
.AutoFilterMode = False
End With
Application.ScreenUpdating = True
End Sub
Bookmarks