Excel 內建的篩選功能無法直接依據儲存格顏色進行篩選,必須透過 VBA 來實現。下列程式碼會隱藏與目前儲存格顏色不同的列(標題列除外)。
Sub FilterColor()
Dim UseRow As Long, AC As Long
UseRow = Cells.SpecialCells(xlCellTypeLastCell).Row
If ActiveCell.Row > UseRow Then
MsgBox "超出範圍,請選擇有資料的儲存格!", vbExclamation, "錯誤"
Else
AC = ActiveCell.Column
Cells.EntireRow.Hidden = False
For i = 2 To UseRow
If Cells(i, AC).Interior.ColorIndex <> ActiveCell.Interior.ColorIndex Then
Cells(i, AC).EntireRow.Hidden = True
End If
Next i
End If
End Sub
FilterColor。如有其他需求(例如保留相同顏色、多欄位判斷),請自行修改程式碼邏輯。