比较单元格并显示图标
我有一个Excelmacros,比较每个单元格与以前,并显示一个更大,更less或平等的图标:
Sub Up() Dim c As Range For Each c In Worksheets("Général").Range("D2:T20") MFC c Next c End Sub Private Sub MFC(ByVal Rng As Range) With Rng .FormatConditions.Delete .FormatConditions.AddIconSetCondition With .FormatConditions(1) .SetFirstPriority .IconSet = ThisWorkbook.IconSets(xl3Arrows) With .IconCriteria(2) .Type = xlConditionValueFormula .Value = "=" & Rng.Offset(, -1).Address .Operator = xlGreaterEqual End With With .IconCriteria(3) .Type = xlConditionValueFormula .Value = "=" & Rng.Offset(, -1).Address .Operator = xlGreater End With End With End With End Sub
实际显示:
我只需要扭转越来越less的图标(跟踪位置:位置1比位置2好)
通缉展示:
考虑一个想法,将xlGreaterEqual切换到xlLessEqual和xlGreater到xlLess我注意到这个MSDN 文章说:
对于图标集条件格式规则,该属性可以设置为以下两个常量中的一个: xlGreater或xlGreaterEqual 。
这是由UI重新强化,这些是可以select条件格式与图标集规则的选项的限制:
因此,这里的解决scheme可能只是将数据符号颠倒过来,然后用数字格式“隐藏”,然后使用已经发布的代码。
所以在这个例子中:
- 第一个范围是你的例子
- 第二个范围是你的例子,每个值是
*-1
- 第三个范围是自定义数字格式为
0;0;0
的第二个范围,使负数看起来是正数。
代码和你的完全一样(我的更改是在我的工作表上的范围); 这是符号切换和数字格式,得到您想要的输出:
Option Explicit Sub Up() Dim rngSource As Range Dim rngCell As Range Set rngSource = ThisWorkbook.Worksheets("Sheet1").Range("B2:E3") For Each rngCell In rngSource MFC rngCell Next rngCell Set rngSource = ThisWorkbook.Worksheets("Sheet1").Range("B6:E7") For Each rngCell In rngSource MFC rngCell Next rngCell Set rngSource = ThisWorkbook.Worksheets("Sheet1").Range("B10:E11") For Each rngCell In rngSource MFC rngCell Next rngCell End Sub Private Sub MFC(ByVal rng As Range) Dim lngThisNumber As Long Dim lngPreviousNumber As Long lngThisNumber = rng.Value lngPreviousNumber = rng.Offset(0, -1).Value With rng .FormatConditions.Delete .FormatConditions.AddIconSetCondition With .FormatConditions(1) .SetFirstPriority .IconSet = ThisWorkbook.IconSets(xl3Arrows) With .IconCriteria(2) .Type = xlConditionValueFormula .Value = "=" & rng.Offset(, -1).Address .Operator = xlGreaterEqual End With With .IconCriteria(3) .Type = xlConditionValueFormula .Value = "=" & rng.Offset(, -1).Address .Operator = xlGreater End With End With End With End Sub
请注意,我试过这个.Value
作业:
.Value = "=" & rng.Offset(, -1).Address & "*-1"
所以当应用格式化时,运行时会执行*-1
,但是,这并没有得到所需的输出。