繁体   English   中英

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

[英]Excel VBA. Convert a list of cells to Ranges

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

    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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM