Select 条件格式中的所有彩色单元格
Select all colored cells from conditional formatting
我的 sheet "Summary" 中有一系列单元格 (K6:M200),我需要使用宏来 select 所有基于条件格式着色的单元格.有以下条件:
- 彩色单元格将从K6开始连续,直到任何一行不满足条件。
- 并非所有单元格的颜色都相同。
我是 VBA 和宏的新手,所以我希望有人可以帮助我了解如何执行此操作。我已经尝试了一些公式,但都没有用。
我建议这样:
Sub selectCFColours()
Dim cell As Range
Dim selRange As Range
For Each cell In Range("K6:M200")
If cell.DisplayFormat.Interior.Color <> cell.Interior.Color Then
If selRange Is Nothing Then
Set selRange = cell
Else
Set selRange = Union(selRange, cell)
End If
End If
Next
If Not selRange Is Nothing Then selRange.Select
End Sub
以下代码用于通用 FindAll 代码。这也可以通过设置 Application.FindFormat 来使用,因此它可以与条件格式一起使用。
Sub FindBlack()
Dim FoundRange As Range
With Application.FindFormat
.Clear
.Interior.Color = RGB(0, 0, 0)
End With
Set FoundRange = FindAll("", LookIn:=xlFormulas, SearchWhat:=Range("K6:M200"), SearchFormat:=True)
If Not FoundRange Is Nothing Then Debug.Print FoundRange.Address
End Sub
Function FindAll(What, _
Optional SearchWhat As Variant, _
Optional LookIn, _
Optional LookAt, _
Optional SearchOrder, _
Optional SearchDirection As XlSearchDirection = xlNext, _
Optional MatchCase As Boolean = False, _
Optional MatchByte, _
Optional SearchFormat) As Range
'LookIn can be xlValues or xlFormulas, _
LookAt can be xlWhole or xlPart, _
SearchOrder can be xlByRows or xlByColumns, _
SearchDirection can be xlNext, xlPrevious, _
MatchCase, MatchByte, and SearchFormat can be True or False. _
Before using SearchFormat = True, specify the appropriate settings for the Application.FindFormat _
object; e.g. Application.FindFormat.NumberFormat = "General;-General;""-"""
Dim SrcRange As Range
If IsMissing(SearchWhat) Then
Set SrcRange = ActiveSheet.UsedRange
ElseIf TypeOf SearchWhat Is Range Then
Set SrcRange = IIf(SearchWhat.Cells.Count = 1, SearchWhat.Parent.UsedRange, SearchWhat)
ElseIf TypeOf SearchWhat Is Worksheet Then
Set SrcRange = SearchWhat.UsedRange
Else: SrcRange = ActiveSheet.UsedRange
End If
If SrcRange Is Nothing Then Exit Function
'get the first matching cell in the range first
With SrcRange.Areas(SrcRange.Areas.Count)
Dim FirstCell As Range: Set FirstCell = .Cells(.Cells.Count)
End With
Dim CurrRange As Range: Set CurrRange = SrcRange.Find(What:=What, After:=FirstCell, LookIn:=LookIn, LookAt:=LookAt, _
SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)
If Not CurrRange Is Nothing Then
Set FindAll = CurrRange
Do
Set CurrRange = SrcRange.Find(What:=What, After:=CurrRange, LookIn:=LookIn, LookAt:=LookAt, _
SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)
If CurrRange Is Nothing Then Exit Do
If Application.Intersect(FindAll, CurrRange) Is Nothing Then
Set FindAll = Application.Union(FindAll, CurrRange)
Else: Exit Do
End If
Loop
End If
End Function
我意识到,因为我知道第一个值,并且我的宏的一部分先前选择了将被着色的整个范围,所以我可以使用这两个值构建一个范围来工作。
所以我做了:
Dim r1 As Range, r2 As Range
Set r1 = Selection
Set r2 = ActiveSheet.Range("K6")
Range(r2, r1).Select
它奏效了。我只是接近这个错误。
我的 sheet "Summary" 中有一系列单元格 (K6:M200),我需要使用宏来 select 所有基于条件格式着色的单元格.有以下条件:
- 彩色单元格将从K6开始连续,直到任何一行不满足条件。
- 并非所有单元格的颜色都相同。
我是 VBA 和宏的新手,所以我希望有人可以帮助我了解如何执行此操作。我已经尝试了一些公式,但都没有用。
我建议这样:
Sub selectCFColours()
Dim cell As Range
Dim selRange As Range
For Each cell In Range("K6:M200")
If cell.DisplayFormat.Interior.Color <> cell.Interior.Color Then
If selRange Is Nothing Then
Set selRange = cell
Else
Set selRange = Union(selRange, cell)
End If
End If
Next
If Not selRange Is Nothing Then selRange.Select
End Sub
以下代码用于通用 FindAll 代码。这也可以通过设置 Application.FindFormat 来使用,因此它可以与条件格式一起使用。
Sub FindBlack()
Dim FoundRange As Range
With Application.FindFormat
.Clear
.Interior.Color = RGB(0, 0, 0)
End With
Set FoundRange = FindAll("", LookIn:=xlFormulas, SearchWhat:=Range("K6:M200"), SearchFormat:=True)
If Not FoundRange Is Nothing Then Debug.Print FoundRange.Address
End Sub
Function FindAll(What, _
Optional SearchWhat As Variant, _
Optional LookIn, _
Optional LookAt, _
Optional SearchOrder, _
Optional SearchDirection As XlSearchDirection = xlNext, _
Optional MatchCase As Boolean = False, _
Optional MatchByte, _
Optional SearchFormat) As Range
'LookIn can be xlValues or xlFormulas, _
LookAt can be xlWhole or xlPart, _
SearchOrder can be xlByRows or xlByColumns, _
SearchDirection can be xlNext, xlPrevious, _
MatchCase, MatchByte, and SearchFormat can be True or False. _
Before using SearchFormat = True, specify the appropriate settings for the Application.FindFormat _
object; e.g. Application.FindFormat.NumberFormat = "General;-General;""-"""
Dim SrcRange As Range
If IsMissing(SearchWhat) Then
Set SrcRange = ActiveSheet.UsedRange
ElseIf TypeOf SearchWhat Is Range Then
Set SrcRange = IIf(SearchWhat.Cells.Count = 1, SearchWhat.Parent.UsedRange, SearchWhat)
ElseIf TypeOf SearchWhat Is Worksheet Then
Set SrcRange = SearchWhat.UsedRange
Else: SrcRange = ActiveSheet.UsedRange
End If
If SrcRange Is Nothing Then Exit Function
'get the first matching cell in the range first
With SrcRange.Areas(SrcRange.Areas.Count)
Dim FirstCell As Range: Set FirstCell = .Cells(.Cells.Count)
End With
Dim CurrRange As Range: Set CurrRange = SrcRange.Find(What:=What, After:=FirstCell, LookIn:=LookIn, LookAt:=LookAt, _
SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)
If Not CurrRange Is Nothing Then
Set FindAll = CurrRange
Do
Set CurrRange = SrcRange.Find(What:=What, After:=CurrRange, LookIn:=LookIn, LookAt:=LookAt, _
SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)
If CurrRange Is Nothing Then Exit Do
If Application.Intersect(FindAll, CurrRange) Is Nothing Then
Set FindAll = Application.Union(FindAll, CurrRange)
Else: Exit Do
End If
Loop
End If
End Function
我意识到,因为我知道第一个值,并且我的宏的一部分先前选择了将被着色的整个范围,所以我可以使用这两个值构建一个范围来工作。
所以我做了:
Dim r1 As Range, r2 As Range
Set r1 = Selection
Set r2 = ActiveSheet.Range("K6")
Range(r2, r1).Select
它奏效了。我只是接近这个错误。