這是一個不完整的企圖,使其儘可能地通用
(爲起點僅提供)
Option Explicit
Private Const SP As String = "||" 'string delimiter, or SeParator
Public Sub x()
resetConditionalFormatting Sheet1.UsedRange
End Sub
Public Sub resetConditionalFormatting(Optional ByRef rng As Range = Nothing)
Const FIRST_ROW As Long = 2
Dim colRng As Range, thisCol As Long, fc As FormatCondition, thisFC As Long
Dim maxCell As Range, ws As Worksheet, cell1 As Range, cell2 As Range
If rng Is Nothing Then Set rng = Application.ActiveWorkbook.ActiveSheet.UsedRange
Set ws = rng.Parent
Set maxCell = GetMaxCell(rng)
If maxCell.Row > 1 Or maxCell.Column > 1 Or Len(maxCell) > 0 Then
thisCol = 1
Set cell1 = ws.Cells(FIRST_ROW, thisCol)
Set cell2 = ws.Cells(maxCell.Row, thisCol)
For Each colRng In rng.Columns
thisFC = 1
For Each fc In colRng.FormatConditions
fc.ModifyAppliesToRange ws.Range(cell1, cell2)
thisFC = thisFC + 1
Next
thisCol = thisCol + 1
Next
End If
End Sub
Private Sub fcDupe(ByRef fc As Variant, ByRef fcType() As String, ByRef dupes As Long)
Dim tStr As String, itm As Variant, fcT As Byte
On Error Resume Next 'some properties may not be defined at runtime
With fc
fcT = .Type
tStr = SP
'Border, Font, and Interior apply to 1, 2, 5, 8, 9, 10, 11, 12, 13, 16, 17
tStr = tStr & CStr(ObjPtr(.Borders)) & _
CStr(ObjPtr(.Font)) & _
CStr(ObjPtr(.Interior))
'CStr(ObjPtr(fc)): https://support2.microsoft.com/default.aspx?scid=kb;en-us;199824
Select Case fcT
Case xlCellValue '1
tStr = tStr & .DateOperator
tStr = tStr & .Formula1
tStr = tStr & .Formula2
tStr = tStr & .Operator
tStr = tStr & .ScopeType
tStr = tStr & .Text
tStr = tStr & .TextOperator
tStr = tStr & SP
Case xlColorScale '3
tStr = SP & CStr(ObjPtr(.ColorScaleCriteria))
tStr = tStr & .Formula
tStr = tStr & .ScopeType
tStr = tStr & SP
Case xlDatabar '4
tStr = SP & CStr(ObjPtr(.AxisColor)) & _
CStr(ObjPtr(.BarBorder)) & _
CStr(ObjPtr(.BarColor)) & _
CStr(ObjPtr(.MaxPoint)) & _
CStr(ObjPtr(.MinPoint)) & _
CStr(ObjPtr(.NegativeBarFormat))
tStr = tStr & .AxisPosition
tStr = tStr & .BarFillType
tStr = tStr & .Direction
tStr = tStr & .Formula
tStr = tStr & .PercentMax
tStr = tStr & .PercentMin
tStr = tStr & .ScopeType
tStr = tStr & .ShowValue
tStr = tStr & SP
Case xlTop10 '5
tStr = tStr & .CalcFor
tStr = tStr & .Percent
tStr = tStr & .Rank
tStr = tStr & .TopBottom
tStr = tStr & .ScopeType
tStr = tStr & SP
Case 6 'XlFormatConditionType.xlIconSet
tStr = SP & CStr(ObjPtr(.IconCriteria)) & CStr(ObjPtr(.IconSet))
tStr = tStr & .Formula
tStr = tStr & .PercentValue
tStr = tStr & .ReverseOrder
tStr = tStr & .ScopeType
tStr = tStr & .ShowIconOnly
tStr = tStr & SP
Case xlUniqueValues '8
tStr = tStr & .DupeUnique
tStr = tStr & .ScopeType
tStr = tStr & SP
Case xlTextString '9
tStr = tStr & .DateOperator
tStr = tStr & .Formula1
tStr = tStr & .Formula2
tStr = tStr & .Operator
tStr = tStr & .ScopeType
tStr = tStr & .Text
tStr = tStr & .TextOperator
tStr = tStr & SP
Case xlAboveAverageCondition '12
tStr = tStr & .AboveBelow
tStr = tStr & .CalcFor
tStr = tStr & .Formula1
tStr = tStr & .Formula2
tStr = tStr & .NumStdDev
tStr = tStr & SP
Case xlExpression, _
xlBlanksCondition, _
xlTimePeriod, _
xlNoBlanksCondition, _
xlErrorsCondition, _
xlNoErrorsCondition
tStr = tStr & .Formula1
tStr = tStr & .Formula2
tStr = tStr & SP
End Select
If InStr(1, fcType(fcT), tStr, vbBinaryCompare) = 0 Then
fcType(fcT) = fcType(fcT) & tStr
Else
.Delete
dupes = dupes + 1
End If
End With
End Sub
Public Function GetMaxCell(Optional ByRef rng As Range = Nothing) As Range
'It returns the last cell of range with data, or A1 if Worksheet is empty
Const NONEMPTY As String = "*"
Dim lRow As Range, lCol As Range
If rng Is Nothing Then Set rng = Application.ActiveWorkbook.ActiveSheet.UsedRange
If WorksheetFunction.CountA(rng) = 0 Then
Set GetMaxCell = rng.Parent.Cells(1, 1)
Else
With rng
Set lRow = .Cells.Find(What:=NONEMPTY, LookIn:=xlFormulas, _
After:=.Cells(1, 1), _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows)
Set lCol = .Cells.Find(What:=NONEMPTY, LookIn:=xlFormulas, _
After:=.Cells(1, 1), _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns)
Set GetMaxCell = .Parent.Cells(lRow.Row, lCol.Column)
End With
End If
End Function
的一種方式,以查看特定格式條件的所有特性:
聽起來好像你想要做的是刪除重複項算多少。條件格式與此有什麼關係?只需定義你的範圍(關於如何做到這一點,大量的關於SO的帖子);計算該範圍內的條目;執行'range.removeduplicates'方法,並重新計數。報告消息框中的差異。如果你不想留下唯一身份證,可能會有所不同,但是從你的帖子中不太清楚。 –
從宏錄製器開始,修改並刪除一些條件格式並使用生成的代碼作爲您的起點。然後編輯您的帖子以包含代碼。 – ChipsLetten
ChipsLetten:感謝您的回覆和雙謝謝理解我的文章!我希望有人可能已經設計了這個工具,並可以給我。我不介意從頭開始,但我認爲在我開始之前詢問是否有人已經「發明了車輪」並不會受傷。 –