1

我设置了一个工作簿,其中 Sheet1 可在多列(C 到 T 列)中编辑 A&B 列中某些列出的名称的到期日期;第 1 行和第 2 行是标题,因此数据输入从第 3 行开始。

Sheet2 使用 INDIRECT 公式作为受保护的页面是相同的,具有条件格式,如果到期日期即将到来,则将某些单元格突出显示为红色或黄色。

我对 VBA 缺乏经验,一直在寻找符合以下条件的宏:

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

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

4

2 回答 2

4

这里有一个小脚本可以帮助您入门。它将遍历每一行的每一列并检查每个单元格的颜色。如果找到任何颜色,则将跳过该行。如果没有找到任何颜色的单元格,则该行将被隐藏。换句话说,所有全白的行都将被隐藏

代码:

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

在此处输入图像描述

在此处输入图像描述

于 2013-03-27T13:14:46.480 回答
0
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
于 2013-03-27T10:10:58.780 回答