Excel:隐藏不包含彩色单元格的所有行和列

我想创build一个macros,当激活时,将隐藏没有格式化为特定颜色的单元格的所有列和行。 我修改了一个只包含内容的列的类似的子集,但是这是另外一个额外的步骤,我的大脑似乎无法在今天上午左右。 作为参考,这是我用来隐藏所有没有内容的列:

Sub HideCols() Dim LC As Integer, j As Integer Dim LR As Integer, curCnt As Integer Dim k As Integer Dim Data As Variant Application.ScreenUpdating = False LC = Cells(3, Columns.Count).End(xlToLeft).Column For j = 3 To LC LR = Cells(Rows.Count, j).End(xlUp).Row curCnt = 0 Data = Range(Cells(1, 1), Cells(LR, LC)) For k = 1 To LR If Rows(k).Hidden = False And Data(k, j) <> "" Then _ curCnt = curCnt + 1 Next k Columns(j).Hidden = curCnt < 2 Next j Application.ScreenUpdating = True End Sub 

以下是如何隐藏黑色单元格的所有行和列。 我相信你可以修改以适应你的需要。

 Sub hide_cell() Dim Rng As Range Dim MyCell As Range Set Rng = Range("A2:d10") For Each MyCell In Rng If MyCell.Interior.ColorIndex = 1 Then MyCell.EntireRow.Hidden = True MyCell.EntireColumn.Hidden = True End If Next MyCell End Sub