【问题标题】:Selecting range of visible cells only with VBA仅使用 VBA 选择可见单元格的范围
【发布时间】:2018-10-04 02:28:18
【问题描述】:

我想在状态跟踪器上使用以下函数“CountCcolor()”。我的目的是能够使用该函数在可见单元格的单个列范围内查找有多少以特定颜色突出显示,例如绿色。

Function CountCcolor(range_data As Range, criteria As Range) As Long

    Dim datax As Range
    Dim xcolor As Long

    ' The next one-liner does not work. Without it, it selects visible and hidden cells. I only want it to select visible cells:
    range_data = Selection.SpecialCells(xlCellTypeVisible).Select

    xcolor = criteria.Interior.ColorIndex
    For Each datax In range_data
        If datax.Interior.ColorIndex = xcolor Then
             CountCcolor = CountCcolor + 1
        End If
    Next datax
End Function

提前感谢您的帮助!

【问题讨论】:

    标签: vba range excel-2010 cell visible


    【解决方案1】:

    不要使用Interior.ColorIndex

    相反,只需使用Interior.Color。我也被这一次难住了。简而言之,ColorIndex 代表一种口味的颜色,不是一种独特的颜色。更多详情请见here

    Function CountCcolor(range_data As Range, criteria as Range) As Long
    
    Dim myRange As Range, myCell As Range, TempCount As Long
    Set myRange = range_data.SpecialCells(xlCellTypeVisible)
    
    For Each myCell In myRange
        If myCell.Interior.Color = criteria.Interior.Color Then
            TempCount = TempCount + 1
        End If
    Next myCell
    
    CountCcolor = TempCount
    
    End Function
    

    【讨论】:

    • 首先感谢您清理我的代码!简短而甜蜜,因为它应该是 :) 不幸的是,我仍然遇到隐藏单元格的问题:我正在为特定的数据组过滤不同的列,并且只需要选择可见单元格 2 列(突出显示绿色。我使用这个有色单元格的计数来计算完成了多少任务。
    • 我可以在 EOD 重新访问 :)
    • 谢谢。到目前为止,我感谢您的所有帮助:)
    • 我发现其他人有隐藏单元格问题here 但在他们的情况下,他们能够通过将相邻的过滤列更改为相同的格式来解决。就我而言,我有:过滤的列是字符串,而我只需要使用可见单元格的列是日期。我尝试将其全部转换为文本,但无济于事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 2017-08-21
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    相关资源
    最近更新 更多