【问题标题】:How to hide entire column that does not contain a cell with a fill如何隐藏不包含填充单元格的整列
【发布时间】:2019-10-04 00:38:33
【问题描述】:

我有一个表格(X x Y),其中随机单元格填充为红色(范围可以从列中填充的所有单元格到一些填充的单元格,再到列中未填充的单元格)。我想隐藏没有填充单元格的列。下面的代码查找填充为红色的单元格并隐藏整个列,无论是否有任何未填充的单元格。我想隐藏没有填充单元格的列在。

 Dim cell As Range
  For Each cell In Selection
  If cell.Interior.Color = vbRed Then 'finds the filled in cells
  Columns(cell.Column).EntireColumn.Hidden = True 'hides the column with filled in cells
  End If
  Next

提前谢谢你。

【问题讨论】:

  • 当你说没有填充单元格的列时。你是什么意思?数据样本(截图会有所帮助)
  • [1.] 循环遍历表格的列[2.] 使用Application.Worksheetfunction.CountA 检查空白单元格。您也可以为此使用Application.WorksheetFunction.CountBlank[3.]隐藏列

标签: excel vba


【解决方案1】:

这将隐藏列中没有红色单元格的列:

  Dim cell As Range
  Dim c as long
  For c = 1 to selection.columns.count

      Dim redFound as Boolean
      redFound = False

      Dim r as Long
      For r = 1 to Selection.Rows.Count

          If Selection.Cells(r,c).Interior.Color = vbRed Then 
              redFound = True
              Exit For
          End If
      Next

      Selection.Columns(c).EntireColumn.Hidden = redFound

   Next

【讨论】:

    【解决方案2】:

    我想也许你需要确定一个变量来记录需要隐藏或不隐藏的状态。这里我命名为'Flag'来记录列是否需要隐藏。

    Sub Hidden_Column()
    Dim Flag As Boolean
    Dim iRow As Integer
    Dim iClm As Integer
    Dim ColorNum
    
    With Sheet1
        For iClm = 1 To .Cells(1, 256).End(xlToLeft).Column
            Flag = True
            For iRow = 1 To .Cells(65536, 1).End(xlUp).Row
                ColorNum = .Cells(iRow, iClm).Interior.ColorIndex
                If ColorNum <> -4142 Then       'no color
                    Flag = False
                    Exit For
                End If
            Next iRow
            If Flag = True Then
                Columns(Cells(iRow, iClm).Column).EntireColumn.Hidden = True
            End If
        Next iClm
    End With
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      相关资源
      最近更新 更多