Excel VBA。将单元格列表转换为范围

标签 excel vba

我正在尝试将具有条件格式的单元格范围作为字符串返回。我已经设法使用以下代码创建了一个具有条件格式的单元格列表。

    Set Data = ActiveSheet.UsedRange
    For Each Cell In Data
        If Cell.FormatConditions.Count > 0 Then
            If (Not ConditionallyFormattedCellsArray) = -1 Then: ReDim ConditionallyFormattedCellsArray(0)
            If (ConditionallyFormattedCellsArray(UBound(ConditionallyFormattedCellsArray)) <> "") Then: ReDim Preserve ConditionallyFormattedCellsArray(UBound(ConditionallyFormattedCellsArray) + 1)
            ConditionallyFormattedCellsArray(UBound(ConditionallyFormattedCellsArray)) = Cell.Address
        End If
    Next Cell

在我的情况下,可能有一个或多个具有条件格式的不同数据范围,我想将所有这些单独的单元格减少到这些范围。

任何建议表示赞赏!

最佳答案

下面是一个快速示例,说明如何通过创建 Union 来实现您的结果。您在给定范围内找到的所有具有条件格式的单元格。

Option Explicit

Sub Example()
    Dim cfCells As Range
    Dim checkCell As Range
    For Each checkCell In Sheet1.UsedRange
        If checkCell.FormatConditions.Count > 0 Then
            Debug.Print checkCell.Address
            If cfCells Is Nothing Then
                Set cfCells = checkCell
            Else
                Set cfCells = Union(cfCells, checkCell)
            End If
        End If
    Next checkCell

    Dim allCFCells As String
    If cfCells Is Nothing Then
        Debug.Print "no conditionally formatted cells found in range " & Sheet1.UsedRange.Address
    Else
        allCFCells = cfCells.Address
        Debug.Print "conditionally formatted cells: " & allCFCells
    End If
End Sub

关于Excel VBA。将单元格列表转换为范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60199730/

相关文章:

vba - Excel VBA : getting zoom level corresponding with FitToPageWide

C# 绘图书

excel - 如何使用 VBA 加速索引匹配功能

database - 如何迁移一个Access数据库以使多个用户可以从任何地方访问

excel - 如何在VBA中应用MATCH()函数?

excel - VBA 宏导致 Excel 崩溃

excel - 数组公式结果连接到单个单元格中

vba - 单击单元格的 Excel 宏取消隐藏下面的行

excel - 为具有匹配文本的单元格着色

Excel:检查所有列是否具有不同的值