VBA-条件格式:Excel 2010不起作用,Excel 2013将起作用
我最近写了一段代码,它检测但不选择一个必须是条件格式的范围,然后调用一个执行该任务的子例程VBA-条件格式:Excel 2010不起作用,Excel 2013将起作用,vba,excel,excel-2010,excel-2013,Vba,Excel,Excel 2010,Excel 2013,我最近写了一段代码,它检测但不选择一个必须是条件格式的范围,然后调用一个执行该任务的子例程 ' Format For Each ws In Results.Sheets Format_em_all ws.Range(ws.Cells(15, 2), ws.Cells(15 + UBound(FreqToCompare), WrittenCells)) Next ws 其中ws声明为工作表,结果声明为工作簿,FreqToCompare是一个不可能为空的数组,Writtencells是一个
' Format
For Each ws In Results.Sheets
Format_em_all ws.Range(ws.Cells(15, 2), ws.Cells(15 + UBound(FreqToCompare), WrittenCells))
Next ws
其中ws声明为工作表,结果声明为工作簿,FreqToCompare是一个不可能为空的数组,Writtencells是一个不等于0的整数
子例程调用:
Sub Format_em_all(RangeToFormat As Range)
Select Case RangeToFormat.Parent.Name
Case "lol"
RangeToFormat.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=2.9"
RangeToFormat.FormatConditions(1).Interior.ColorIndex = 3
RangeToFormat.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, Formula1:="=2.00001", Formula2:="=2.9"
RangeToFormat.FormatConditions(2).Font.ColorIndex = 3
Case "rofl"
RangeToFormat.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, Formula1:="=-4", Formula2:="=4"
RangeToFormat.FormatConditions(1).Interior.ColorIndex = 3
End Select
End Sub
我用Excel 2013在我的电脑上执行了代码,一切都很顺利。10分钟前,一位同事过来说我的宏不工作。。。我们一起用Excel 2010检查他的电脑。。。是的,它不起作用。它被卡在Case lol之后的第一行,出现运行时错误5-过程调用或参数无效。
我试过了?RangeToFormat.Address,并显示正确的范围地址。。。有什么问题吗?这个答案的功劳全归于阿克塞尔·里克特:
Sub Format_em_all(RangeToFormat As Range)
Select Case RangeToFormat.Parent.Name
Case "lol"
RangeToFormat.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:=2.9
RangeToFormat.FormatConditions(1).Interior.ColorIndex = 3
RangeToFormat.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, Formula1:=2.00001, Formula2:=2.9
RangeToFormat.FormatConditions(2).Font.ColorIndex = 3
Case "rofl"
RangeToFormat.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, Formula1:=-4, Formula2:=4
RangeToFormat.FormatConditions(1).Interior.ColorIndex = 3
End Select
End Sub
正如你所看到的,公式不再是公式,而是整数!这是完美无瑕的您的错误是什么?神奇的数字是你所期望的吗?例如,xlCellValue=1吗?这个范围内已经有条件格式了吗?你所说的卡住是什么意思?请说明错误,如果没有错误,请详细说明观察到的结果与您的预期不同。添加错误说明。如果已经存在条件格式,Excel 2013和Excel 2010在处理条件格式方面是否会有所不同?事实上不是,文件在打开时是空白的,在调用此子例程之前没有条件格式。我怀疑问题不是Excel 2010与2013,而是不同的十进制分隔符。公式的类型是可变的。因此,请尝试将公式设置为值而不是字符串。示例:RangeToFormat.FormatConditions.Add类型:=xlCellValue,运算符:=xlGreater,公式1:=2.9和RangeToFormat.FormatConditions.Add类型:=xlCellValue,运算符:=xlBetween,公式1:=2.00001,公式2:=2.9