Excel VBA公式中的条件格式公式
我被卡住了,我不知道是否有任何解决方案,因为我已经尝试过很多并且一直在失败。Excel VBA公式中的条件格式公式
我试图使细胞在B列变为黄色添加条件格式,如果它等于或它的不到19%,在A列伴侣细胞,所以在上面的例子中,B1单元格应因为19,000美元不到或等于100,000美元的19%。
我需要通过excel vba添加这个条件格式。我尝试添加下面的vba代码,但B1:B3中的所有单元格的条件格式公式都与$A1*0.19
卡住了。我需要B1条件格式化公式为$A1*0.19
,那么B2条件格式化公式将是$A2*0.19
等等。我有大约350行的方式不只是3.即使如此,我的vba代码成为$A521325*0.19
或某种方式的真实或实际。
With Sheet1.Range(Sheet1.Cells(1, 2), Sheet1.Cells(3, 2))
daformula = "=$A1*0.19"
.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, Formula1:=daformula
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
.FormatConditions(1).Interior.ThemeColor = xlThemeColorAccent2
.FormatConditions(1).Interior.TintAndShade = -0.249946592608417
.FormatConditions(1).StopIfTrue = False
End With
的想法是运行的添加条件格式到片材中,当用户改变单元中的一个在列B中的颜色或者消失或根据值再现用户变更的小区到宏后(条件格式必须仍然有效)
尝试,
With ActiveSheet.Cells(1, 2).Resize(3, 1)
.FormatConditions.Add Type:=xlExpression, Formula1:="=$B1<=($A1*0.19)"
.FormatConditions(.FormatConditions.Count).Interior.Color = 65535
End With
无论你添加像.SetFirstPriority
或.StopIfTrue
其他细节将在另一块CF规则如何影响同一细胞某种程度上取决于。
你可以创建一个监视A列中的任何改变(发生这种代码在工作表Sheet1对象)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
'run code to add conditional formatting
condFormat Target
End If
End Sub
现在,我们只需要改变你的上面的代码以接受目标和只添加格式化事件在B列的等效电池
Public sub condFormat (Target as range)
With target.offset(0,1)
daformula = "=" & target.address & "*0.19"
.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:=daformula
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).Interior.PatternColorIndex = xlAutomatic
.FormatConditions(1).Interior.ThemeColor = xlThemeColorAccent2
.FormatConditions(1).Interior.TintAndShade = -0.249946592608417
.FormatConditions(1).StopIfTrue = False
End With
end sub
我没有占一次改变超过1个细胞,但是这将解决你的挑战
我忘了说,整个数据库正在Excel VBA中处理,然后上传到Google Docs。这就是为什么我们需要CF,因为在上传到Google文档时也包含CF – Jay 2014-10-31 18:31:44
当肌酐从VBA条件格式化,相对于条件格式公式的单元格选择至关重要。
尝试使用ActiveSheet.cells(1,1).select如果条件格式公式只访问同一行上的值,或者ActiveSheet.cells(2,1)如果它引用上述行的值。
我测试了你的上面的代码,它工作的很好。正常工作[这里](https://www.dropbox.com/s/er3imeavayvrkmo/Test.xlsm?dl=0) – 2014-11-03 15:43:36
不,我恐怕没有。在链接中使用该文件,尝试删除所有条件格式,然后再次运行代码。你会看到38个中的200个不会变红,其他的也不变。它搞砸了。这看起来确实很难:s – Jay 2014-11-03 20:51:36
不知道该告诉你什么,代码对于Excel 2010是正确的。我想如果你有不同的版本,我没有太大的帮助:( – 2014-11-03 20:57:12