Skip to main content

如何根據儲存格顏色在 Excel 中隱藏列?

Author: Xiaoyang Last Modified: 2025-05-12
A screenshot showing a list of colored cells to hide rows in Excel based on cell color

在 Excel 工作表中,我有一個包含多種不同顏色的儲存格清單,現在,我需要像以下螢幕截圖所示那樣隱藏所有淺藍色的列,該如何快速輕鬆地在 Excel 中完成此任務呢?

使用 VBA 程式碼根據儲存格顏色隱藏列

使用 Kutools for Excel 根據儲存格顏色隱藏列


使用 VBA 程式碼根據儲存格顏色隱藏列

若要根據特定背景顏色隱藏列,以下 VBA 程式碼可能會對您有所幫助,請按照以下步驟操作:

1. 按住 ALT + F11 鍵,然後會打開 Microsoft Visual Basic for Applications 視窗。

2. 點擊 插入 > 模組,並將以下程式碼貼到 模組 視窗中。

VBA 程式碼:根據儲存格顏色隱藏列:

Sub Hidebycolor()
'Updateby Extendoffice
    Dim xRg As Range
    Dim xTxt As String
    Dim xCell As Range
    Dim I As Long
    On Error Resume Next
    If ActiveWindow.RangeSelection.Count > 1 Then
      xTxt = ActiveWindow.RangeSelection.Columns(1).AddressLocal
    Else
      xTxt = ActiveSheet.UsedRange.Columns(1).AddressLocal
    End If
LInput:
    Set xRg = Application.InputBox("Range:", "Kutools for Excel", xTxt, , , , , 8)
    If xRg Is Nothing Then Exit Sub
    If xRg.Areas.Count > 1 Then
        MsgBox "does not support multiple selections", vbInformation, "Kutools for Excel"
        GoTo LInput
    End If
    If xRg.Columns.Count > 1 Then
        MsgBox "the selection is not more than 1 column", vbInformation, "Kutools for Excel"
        GoTo LInput
    End If
    On Error Resume Next
    Application.ScreenUpdating = False
    For I = 1 To xRg.Count
        If xRg.Range("A" & I).Interior.ColorIndex = 20 Then
            xRg.Range("A" & I).EntireRow.Hidden = True
        End If
    Next 'i
    Application.ScreenUpdating = True
End Sub

3. 然後按 F5 鍵執行此程式碼,會彈出一個提示框提醒您選擇要使用的儲存格,請參閱以下螢幕截圖:

A screenshot showing the prompt box to select cells in Excel when running VBA code to hide rows by color

4. 然後點擊 確定 按鈕,所有淺藍色的儲存格將立即被隱藏,請參閱以下螢幕截圖:

A screenshot showing a list of colored cells to hide rows in Excel based on cell color

注意:

1. 在上述程式碼中, If xRg.Range("A" & I).Interior.ColorIndex = 20 這段腳本中的數字 20 是顏色索引號,您可以根據需要更改它。

2. 如果您不確定顏色索引號,則應應用以下程式碼來獲取您想知道的顏色索引:

Function GetColor(r As Range) As Integer
GetColor = r.Interior.ColorIndex
End Function

然後輸入公式:=GetColor(A2) 以返回 A2 儲存格背景的特定顏色索引號,您可以根據需要將 A2 更改為其他儲存格。


使用 Kutools for Excel 根據儲存格顏色隱藏列

如果您有 Kutools for Excel,可以使用其 選擇類似格式單元格 功能,先選擇特定顏色的儲存格,然後一次性將它們隱藏起來。

Kutools for Excel 提供超過 300 種進階功能,簡化複雜任務,提升創造力與效率。 結合 AI 能力,Kutools 能夠精準自動化任務,讓數據管理變得輕而易舉。Kutools for Excel 的詳細資訊...免費試用...

安裝 Kutools for Excel 後,請按照以下步驟操作:

1. 點擊 Kutools > 選擇 > 選擇類似格式單元格,請參閱以下螢幕截圖:

A screenshot of the Select Cells with Format option on the Kutools tab on the Ribbon

2. 在 選擇類似格式單元格 對話框中,進行以下操作:

(1.) 點擊 Range selection button 按鈕選擇您要使用的資料區域;

(2.) 然後點擊 從單元格選擇格式 按鈕,選擇您想基於其隱藏行的特定顏色儲存格;

(3.) 特定儲存格的所有屬性都已列出在列表框中,請僅勾選 填充 部分下的 背景色

A screenshot of the Select Cells with Format dialog box

3. 然後點擊 確定 按鈕,所有具有特定顏色的儲存格將立即被選中,然後點擊 首頁 > 格式 > 隱藏和取消隱藏 > 隱藏行,請參閱以下螢幕截圖:

A screenshot of the Hide Rows option on the Format menu under the Home tab in Excel

4. 然後,所選的具有特定顏色的儲存格已被隱藏,請參閱以下螢幕截圖:

A screenshot showing hidden rows in Excel after using Kutools

Kutools for Excel - 超過 300 種必備工具,讓 Excel 功能更強大。永久免費享受 AI 功能!立即獲取


示範:使用 Kutools for Excel 根據儲存格顏色隱藏列

Kutools for Excel:超過 300 種實用工具任您使用!永久免費享受AI功能!立即下載!

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!