Excel:如果该行中的单元格没有着色,则隐藏一行

我有一个工作簿设置到Sheet1可以在列A和B中的某些列出的名称的多个列(列C到T)的到期date; 行1和2是一个标题,所以数据input从第3行开始。

使用INDIRECT公式作为带有条件格式的受保护页面的Sheet2是相同的,如果到期date到来,则强调某些单元格为红色或黄色。

我对VBA没有经验,一直在寻找符合以下标准的macros:

仅在Sheet2上,如果该行不包含任何红色或黄色的单元格,则隐藏这些非彩色的行。

任何帮助将不胜感激。 我只发现了基于单列标准隐藏行的代码。

这是一个小脚本,让你开始。 它将遍历每一行的每一列,并检查每个单元格的颜色。 如果find任何颜色,该行将被跳过。 如果没有发现有任何颜色的单元格,该行将被隐藏。 换句话说, 所有完全白色的行将被隐藏

码:

Public Sub HideUncoloredRows() Dim startColumn As Integer Dim startRow As Integer Dim totalRows As Integer Dim totalColumns As Integer Dim currentColumn As Integer Dim currentRow As Integer Dim shouldHideRow As Integer startColumn = 1 'column A startRow = 1 'row 1 totalRows = Sheet2.Cells(Rows.Count, startColumn).End(xlUp).Row For currentRow = totalRows To startRow Step -1 shouldHideRow = True totalColumns = Sheet2.Cells(currentRow, Columns.Count).End(xlToLeft).Column 'for each column in the current row, check the cell color For currentColumn = startColumn To totalColumns 'if any colored cell is found, don't hide the row and move on to next row If Not Sheet2.Cells(currentRow, currentColumn).Interior.ColorIndex = -4142 Then shouldHideRow = False Exit For End If Next If shouldHideRow Then 'drop into here if all cells in a row were white Sheet2.Cells(currentRow, currentColumn).EntireRow.Hidden = True End If Next End Sub 

之前

在这里输入图像说明

在这里输入图像说明

 row = 1 do flag = false col = 1 do .cells(row,col).select if selection.interior.colorindex <> vbWhite then flag = true loop until (col = lastcol) or (flag = true) if flag then rows(row).delete row = row - 1 end if row = row + 1 loop until row = lastrow