简体   繁体   English

Excel 2007-VBA-根据单元格值在范围上应用条件格式

[英]Excel 2007 - VBA - Apply Conditional Format on Range Based on Cell Values

For some reason, I can't figure out why the same formula that I can apply to conditional formatting regularly doesn't work in VBA. 由于某些原因,我无法弄清楚为什么我可以定期将相同的公式应用于条件格式在VBA中不起作用。 I'm trying to highlight the row in that range if one of the cells in each row is not blank. 如果每行中的单元格之一不为空,则尝试突出显示该范围内的行。

Below is the code I'm working with: 以下是我正在使用的代码:

With Range("$A3:$L1000")
.FormatConditions.Delete
.FormatConditions.Add xlExpression, Formula1:="=($J3)<>"""""
.FormatConditions(1).Interior.ColorIndex = 46

End With

When it applies to the document, here's the formula in Conditional Formatting 适用于文档时,这是条件格式中的公式 在此处输入图片说明 在此处输入图片说明

Any help/advice you all could provide would be greatly appreciated. 大家都可以提供的任何帮助/建议将不胜感激。

Update: 更新:

I solved this by recording a macro where I assigned the conditional formatting to a single line, copied the formatting, then pasted it over the active range. 我通过记录一个宏来解决这个问题,在宏中我将条件格式分配给了一行,复制了格式,然后将其粘贴到活动范围内。

Thanks to everyone for your input, and hope this helps someone else down the road! 感谢大家的投入,希望这对以后的人有所帮助!

Code

Range("A3:M3").Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=($J3)<>"""""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.Copy
    Range(Selection, Selection.End(xlDown)).Select
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA Excel根据唯一值将条件格式应用于单元格 - VBA Excel Apply Conditional Format to Cells Based on Unique Values Excel 2007-条件格式-基于单元格区域中的值格式化单元格 - Excel 2007- Conditional Formatting- Formatting a cell based on values in a range of cells 在 Excel/VBA 中,如何根据单元格值将公式应用于范围? - In Excel/VBA, how to apply a formula to a range based on cell value? 在Excel 2007条件格式中,如何根据锁定列的单元格内容来格式化整个行? - In Excel 2007 conditional formatting, how to format an entire row based on the content of a cell of a locked column? Excel VBA - 将条件格式公式应用于范围 - Excel VBA - apply conditional formatting formula to range 使用VBA根据单元格值在Excel中隐藏列范围 - Hiding Range of Columns in Excel using VBA based on cell Values Excel VBA-基于单元格范围值从SQL获取数据 - Excel VBA - Get Data from SQL based of Cell range values (Excel) 有没有办法根据另一个单元格的值从一系列单元格中对单元格进行条件格式设置? - (Excel) Is there a way to conditional format a cell from a range of cells based on the value of an another cell? 在excel 2007 vba中应用用户定义的格式 - apply user-defined format in excel 2007 vba Excel VBA If Then函数取决于单元格值 - Excel VBA If Then Function conditional on cell values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM