In my case, the rows should be hidden when the cell in column C is gray (colorindex = 15). But the row should be shown again, when the cell changes color to white again. Is this possible?
In Excel worksheet, I have a list of cells which have many different colors, now, I need to hide all light blue colored rows as following screenshot shown, how could you deal with this task in Excel quickly and easily?
To hide rows based on a specific background color, the following VBA code may help you, please do as this:
1. Hold down the ALT + F11 keys, then it opens the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following code in the Module Window.
VBA code: Hide rows based on cell color:
Sub Hidebycolor() 'Updateby Extendoffice 21060725 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. Then press F5 key to run this code, and a prompt box is popped out to remind you to select the cells that you want to use, see screenshot:
4. And then click OK button, all the light blue colored cells have been hidden at once, see screenshot:
1. In the above code, the number 20 within this script If xRg.Range("A" & I).Interior.ColorIndex = 20 Then is the color index number, you can change it as you need.
2. If you are not sure the color index number, you should apply the following code to get the color index which you want to know:
Function GetColor(r As Range) As Integer GetColor = r.Interior.ColorIndex End Function
And then enter the formula: =GetColor(A2) to return the specific color index number of the background in cell A2, you change A2 to others as you need.
If you have Kutools for Excel, with its Select Cells with Format utility, you can select the specific colored cells first, and then hide them at once.
|Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 60 days.|
After installing Kutools for Excel, please do as follows:
1. Click Kutools > Select > Select Cells with Format, see screenshot:
2. In the Select Cells with Format dialog box, do the below operations:
(1.) Click button to select the data range that you want to use;
(2.) Then click Choose Format From Cell button to select the specific colored cell that you want to hide rows based on;
(3.) All attributes of the specific cell have been listed into the list box, please just check Background Color under the Fill section.
3. Then click Ok button, all cells with the specific color have been selected at once, and then click Home > Format > Hide & Unhide > Hide Rows, see screenshot:
4. And then the selected cells with the specific color are hidden, see screenshot: