[Excel 2003中的VBA(条件格式)工作代码,在Excel 2020中不起作用

问题描述 投票:0回答:1

将条件格式应用于范围的基本代码,当range.value小于7,2时,它将保持白色,而当大于8,1时,它将变为红色。这段代码在我的Excel 2003 Macro-Enabled Workbook文档中运行得很好,但是当我让我的兄弟在他的工作计算机中使用Excel 2020打开它时,会引发此错误

运行时错误'5':无效的过程调用或参数

Private Sub totalEPS(mySelection As Range)
    With mySelection.FormatConditions
        .Delete
        With .Add(Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="=7,2")
            .Interior.Color = 65535
            .StopIfTrue = False
        End With
        With .Add(Type:=xlCellValue, Operator:=xlGreater, Formula1:="=8,1")
            .Interior.Color = 255
            .StopIfTrue = False
        End With
    End With
End Sub

当他点击Debug时,它会停在行中

        With .Add(Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="=7,2")

我无法调试它,因为它不会在我的计算机上仅在他的计算机上引发任何错误。

该代码使用该方法超过50次,在我的笔记本电脑上运行了50次,在第一次运行时崩溃。老实说,我不知道怎么了。

excel vba conditional-formatting excel-2003
1个回答
3
投票

我怀疑这与小数点分隔符有关-逗号在美国操作系统上将是一个点。

尝试将此功能添加到您的模块中:

Public Function LocalizeDecimal(ByVal value As Double) As String
    LocalizeDecimal = Replace(CStr(value), ".", Application.International(xlDecimalSeparator))
End Function

然后将Formula1参数编辑为以下内容:

With .Add(Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="=" & LocalizeDecimal(7.2))

和:

With .Add(Type:=xlCellValue, Operator:=xlGreater, Formula1:="=" & LocalizeDecimal(8.1))
© www.soinside.com 2019 - 2024. All rights reserved.