Sub FilterAndSum()
' Declare variables
Dim wsSource As Worksheet, wsResult As Worksheet
Dim lastRow As Long, lastRowResult As Long
Dim storeList As Collection
Dim brandList As Collection
Dim resultTable As Collection
Dim i As Long
' Set the source worksheet
Set wsSource = ThisWorkbook.Worksheets("Sheet1")
' If the result worksheet doesn't exist, create it
On Error Resume Next
Set wsResult = ThisWorkbook.Worksheets("Result")
If wsResult Is Nothing Then
Set wsResult = ThisWorkbook.Worksheets.Add
wsResult.Name = "Result"
End If
On Error GoTo 0
' Initialize the output table and add the headers
Set resultTable = New Collection
lastRowResult = 1
wsResult.Cells(lastRowResult, 1) = "门店"
wsResult.Cells(lastRowResult, 2) = "品牌"
wsResult.Cells(lastRowResult, 3) = "销售金额"
' Get the unique store and brand list
Set storeList = GetUniqueValues(wsSource.Range("B:B"))
Set brandList = GetUniqueValues(wsSource.Range("H:H"))
' Loop through each store and brand, and calculate the sales amount
For Each store In storeList
For Each brand In brandList
' Apply filters on store and brand columns
wsSource.Rows(1).AutoFilter Field:=2, Criteria1:=store
wsSource.Rows(1).AutoFilter Field:=8, Criteria1:=brand
' Sum up the sales amount
lastRow = wsSource.Cells(wsSource.Rows.Count, 11).End(xlUp).Row
salesAmount = Application.WorksheetFunction.Subtotal(109, wsSource.Range("K2:K" & lastRow))
' Add the result to the result table
If salesAmount > 0 Then
lastRowResult = lastRowResult + 1
wsResult.Cells(lastRowResult, 1) = store
wsResult.Cells(lastRowResult, 2) = brand
wsResult.Cells(lastRowResult, 3) = salesAmount
End If
Next brand
Next store
' Remove filters from the source worksheet
On Error Resume Next
wsSource.AutoFilterMode = False
On Error GoTo 0
MsgBox "计算完成"
End Sub
Function GetUniqueValues(rng As Range) As Collection
Dim cell As Range
Dim uniqueList As Collection
Set uniqueList = New Collection
On Error Resume Next
For Each cell In rng
If cell.Value <> "" Then
uniqueList.Add cell.Value, CStr(cell.Value)
End If
Next cell
On Error GoTo 0
Set GetUniqueValues = uniqueList
End Function
Sub FilterAndSum()
' Declare variables
Dim wsSource As Worksheet, wsResult As Worksheet
Dim lastRow As Long, lastRowResult As Long
Dim storeList As Collection
Dim brandList As Collection
Dim resultTable As Collection
Dim store As Variant
Dim brand As Variant
Dim salesAmount As Double
' Set the source worksheet
Set wsSource = ThisWorkbook.Worksheets("Sheet1")
' If the result worksheet doesn't exist, create it
On Error Resume Next
Set wsResult = ThisWorkbook.Worksheets("Result")
If wsResult Is Nothing Then
Set wsResult = ThisWorkbook.Worksheets.Add
wsResult.Name = "Result"
End If
On Error GoTo 0
' Initialize the output table and add the headers
Set resultTable = New Collection
lastRowResult = 1
wsResult.Cells(lastRowResult, 1) = "门店"
wsResult.Cells(lastRowResult, 2) = "品牌"
wsResult.Cells(lastRowResult, 3) = "销售金额"
' Get the unique store and brand list
Set storeList = GetUniqueValues(wsSource.Range("B:B"))
Set brandList = GetUniqueValues(wsSource.Range("H:H"))
' Loop through each store and brand, and calculate the sales amount
For Each store In storeList
For Each brand In brandList
' Apply filters on store and brand columns
wsSource.Rows(1).AutoFilter Field:=2, Criteria1:=store
wsSource.Rows(1).AutoFilter Field:=8, Criteria1:=brand
' Sum up the sales amount
lastRow = wsSource.Cells(wsSource.Rows.Count, 11).End(xlUp).Row
salesAmount = Application.WorksheetFunction.Subtotal(109, wsSource.Range("K2:K" & lastRow))
' Add the result to the result table
If salesAmount > 0 Then
lastRowResult = lastRowResult + 1
wsResult.Cells(lastRowResult, 1) = store
wsResult.Cells(lastRowResult, 2) = brand
wsResult.Cells(lastRowResult, 3) = salesAmount
End If
Next brand
Next store
' Remove filters from the source worksheet
On Error Resume Next
wsSource.AutoFilterMode = False
On Error GoTo 0
MsgBox "计算完成"
End Sub
Function GetUniqueValues(rng As Range) As Collection
Dim cell As Range
Dim uniqueList As Collection
Set uniqueList = New Collection
On Error Resume Next
' 修改此循环以仅遍历有数据的单元格
For Each cell In rng.SpecialCells(xlCellTypeConstants)
If cell.Value <> "" Then
uniqueList.Add cell.Value, CStr(cell.Value)
End If
Next cell
On Error GoTo 0
Set GetUniqueValues = uniqueList
End Function