[英]How to highlight cells when using Excel VBA conditional formatting?
Okay so I am stuck and would appreciate any help or advice at all. 好的,我很坚持,不胜感激。
I am trying to highlight the cells that meet two conditions. 我试图强调满足两个条件的单元。 That is to say, in this particular case if the absolute value in a cell is greater than 3 and this same absolute value is greater than another value in an adjacent cell then the cell gets highlighted.
也就是说,在此特定情况下,如果一个单元格中的绝对值大于3,并且此相同的绝对值大于相邻单元格中的另一个值,则该单元格会突出显示。 So for example, my data looks like this:
因此,例如,我的数据如下所示:
-1.2, 1.71, 6.26, 10.2, 3.3, 0.4 4 -4 -1.2、1.71、6.26、10.2、3.3、0.4 4 -4
So if the absolute values of any of the six values on the left are greater than 3 and also greater than four (the adjacent four on the right) then they get highlighted. 因此,如果左侧六个值中的任何一个的绝对值都大于3且还大于四个(右侧的相邻四个),则它们将突出显示。 The adjacent value will change and not always be four.
相邻的值将更改,并且并不总是为4。 I need to do this in VBA and I want the code to work in such a way that when I move to another line of data similar to the one above the two conditions are tested and the correct values are highlighted.
我需要在VBA中执行此操作,并且我希望代码以这样的方式工作:当我移至与上述行相似的另一行数据时,将测试这两个条件并突出显示正确的值。 So this is what I have written so far in VBA.
这就是我到目前为止在VBA中编写的内容。
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(ABS(RC) >3,ABS(RC) >RC[9])"
Selection.FormatConditions(1).Interior.ColorIndex = 45
The problem: 问题:
I am using the R1C1 format as I want it to update automatically when I apply this code to another row of data. 我正在使用R1C1格式,因为我希望将此代码应用于另一行数据时自动更新。 But currently this code only highlights the number 10.2 and not the 6.26 as well as it should.
但是目前,此代码仅突出显示数字10.2,而不是突出显示6.26。 I think that if I leave the code as ABS(RC) this will refer to the current cell value?
我认为,如果我将代码保留为ABS(RC),将引用当前的单元格值吗? Please advise.
请指教。 Also the number 4 (adjacent cell) is nine columns from the first value -1.2 - hence me writing RC[9].
同样,数字4(相邻单元格)距离第一个值-1.2九列-因此我写了RC [9]。
From what I understand you are trying to change reference for 1 condition with each cell but keep the reference for the 2nd condition (checking value of each cell and also checking its value against fixed last cell). 据我了解,您正在尝试为每个单元格更改1个条件的引用,但保留第二个条件的引用(检查每个单元格的值,并针对固定的最后一个单元格检查其值)。
I used 我用了
Sub highlight()
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(ABS(RC) >3,ABS(RC) > abs(indirect(address(" & Selection.Row & "," & _
(Selection.Column + Selection.Columns.Count - 1) & "))))"
Selection.FormatConditions(1).Interior.ColorIndex = 45
End Sub
1st I select range A1:H1 (that's where I keep your numbers) then I run this macro and I end up with 6.26 and 10.2 highlighted. 首先,我选择范围A1:H1(这是保存您的数字的地方),然后运行此宏,最终得到6.26和10.2突出显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.