excel - Excel VBA 不能创建超过 3 个条件格式规则?
问题描述
我尝试使用 VBA 创建一些条件格式规则,但我不断收到第三条规则的“下标超出范围”错误。
这是我当前的代码
Dim MyRange As Range
Dim MyRange2 As Range
Dim MyRange3 As Range
'Define the range for the respective rules
Set MyRange = Range("M4:N30")
Set MyRange2 = Range("A4:Z30")
Set MyRange3 = Range("O4:P30")
MyRange2.FormatConditions.Delete
'Rule 1
MyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=$M4<>""NA""", Formula2:="=$N4<>""NA"""
MyRange.FormatConditions(1).Interior.Color = RGB(255, 150, 0)
'Rule 2
MyRange2.FormatConditions.Add Type:=xlExpression, Formula1:="=$O4>56", Formula2:="=$P4>56"
MyRange2.FormatConditions(2).Interior.Color = RGB(255, 255, 0)
'Rule 3
MyRange3.FormatConditions.Add Type:=xlExpression, Formula1:="=$O4<>$P4"
MyRange3.FormatConditions(3).Interior.Color = RGB(255, 255, 0)
如果我将 FormatConditions(3) 中的“3”更改为 1 或 2,则不会出现错误。关于导致错误的原因有什么建议吗?
感谢您的时间
解决方案
你应该使用:
MyRange3.FormatConditions(2).Interior.Color = RGB(255, 255, 0)
因为对于指定的范围,即O4:P30
它是第二条规则,它会给你错误,因为规则号 3 不存在!
推荐阅读
- sql - 将 FileSystem::\\ 添加到文件路径会从 SQL 导出一个空白 CSV
- python - 从 twitter 数据中删除表情符号/图像十六进制代码
- reporting-services - 对现有 ssrs 报告的数据集的更改
- google-apps-script - 根据表单响应和时间戳发送自动电子邮件
- css - 在列中拆分时使用 flexbox 等高“行”
- wordpress - 什么会导致本地服务器显示“/的索引”而不是 wordpress 站点?
- powershell - 如何使用 powershell 在 .config 文件中添加额外的行
- typescript - 异步/等待同步谷歌地图海拔请求
- qt - 是否可以像在 Qwt 中那样使用 QtCharts 创建滑动 x 轴?
- c - 无法使用 I2C 和 Arduino 写入 M24256 EEPROM