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