Excel VBA-格式条件

Excel VBA-格式条件,excel,vba,Excel,Vba,我想在我的应用程序中使用以下vba代码 .FormatConditions.Add Type:=xlExpression, Formula1:="=Mod(Column(), 2)" 我犯了个错误 错误号5。无效参数 那个代码有什么问题? THX 这是我在sub.中的完整代码。。除最后两种情况外,所有情况都很好 ' FormatConditions With Range("K6:BH" & lastUsedRow) .FormatConditions.Delete

我想在我的应用程序中使用以下vba代码

.FormatConditions.Add Type:=xlExpression, Formula1:="=Mod(Column(), 2)"
我犯了个错误

错误号5。无效参数

那个代码有什么问题? THX

这是我在sub.中的完整代码。。除最后两种情况外,所有情况都很好

    ' FormatConditions
With Range("K6:BH" & lastUsedRow)
    .FormatConditions.Delete
    ' Prozent
    .FormatConditions.Add Type:=xlExpression, Formula1:="=Prozent"
    .FormatConditions(1).StopIfTrue = False
    .FormatConditions(1).Interior.Pattern = xlNone
    .FormatConditions(1).Interior.Color = RGB(174, 170, 170)
    With .FormatConditions(1).Borders(xlBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 2
        .Weight = xlThin
    End With
    ' Prozent unter
    .FormatConditions.Add Type:=xlExpression, Formula1:="=ProzentUnter"
    .FormatConditions(2).StopIfTrue = False
    .FormatConditions(2).Interior.Color = RGB(255, 192, 0)
    With .FormatConditions(2).Borders(xlBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 2
        .Weight = xlThin
    End With
    ' ist
    .FormatConditions.Add Type:=xlExpression, Formula1:="=Ist"
    .FormatConditions(3).StopIfTrue = False
    .FormatConditions(3).Interior.Color = RGB(208, 206, 206)
    .FormatConditions(3).Interior.Pattern = xlLightUp
    .FormatConditions(3).Interior.PatternColor = RGB(68, 84, 106)
    With .FormatConditions(3).Borders(xlBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 2
        .Weight = xlThin
    End With
    ' ist unter
    .FormatConditions.Add Type:=xlExpression, Formula1:="=IstUnter"
    .FormatConditions(4).StopIfTrue = False
    .FormatConditions(4).Interior.Color = RGB(255, 192, 0)
    .FormatConditions(4).Interior.Pattern = xlLightUp
    .FormatConditions(4).Interior.PatternColor = RGB(68, 84, 106)
    With .FormatConditions(4).Borders(xlBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 2
        .Weight = xlThin
    End With
    ' Plan
    .FormatConditions.Add Type:=xlExpression, Formula1:="=Planen"
    .FormatConditions(5).StopIfTrue = False
    .FormatConditions(5).Interior.Color = RGB(255, 255, 255)
    .FormatConditions(5).Interior.Pattern = xlLightUp
    .FormatConditions(5).Interior.PatternColor = RGB(68, 84, 106)
    With .FormatConditions(5).Borders(xlBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 2
        .Weight = xlThin
    End With
    ' timee
    .FormatConditions.Add Type:=xlExpression, Formula1:="=K$5=$F$1"
    .FormatConditions(6).StopIfTrue = False
    .FormatConditions(6).Interior.Color = RGB(198, 224, 180)
    With .FormatConditions(6).Borders(xlLeft)
        .LineStyle = xlContinuous
        .Color = RGB(209, 136, 27)
        .Weight = xlThin
    End With
    With .FormatConditions(6).Borders(xlRight)
        .LineStyle = xlContinuous
        .Color = RGB(209, 136, 27)
        .Weight = xlThin
    End With

    .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(COLUMN(), 2)"
    .FormatConditions(7).Interior.Color = RGB(242, 242, 242)
    '.FormatConditions.Add Type:=xlExpression, Formula1:="=Mod(Column(), 2)=0"
    '.FormatConditions(8).Interior.Color = RGB(255, 255, 255)
End With

原因是计算机上的区域设置。应该使用
分号来分隔公式参数,而不是
逗号
。因此,不是这一行:

.FormatConditions.Add Type:=xlExpression, Formula1:="=Mod(Column(), 2)"
使用这个:

.FormatConditions.Add Type:=xlExpression, Formula1:="=Mod(Column(); 2)"

或者您也可以更改您的区域设置。

已经发布了一个非常好的答案,但作为补充:

使用
Application.International(xlListSeparator)


无论区域设置如何,这都应该有效。

我测试了您的代码,效果很好。你必须给我们足够的信息来重现这个问题,否则我们无能为力。请参阅以下指南:@Jean FrançoisCorbett我编辑了我的帖子-这是我的完整代码;)经过测试,没有错误。请你读一下:是的,我可以。我在两台计算机上测试了这两行代码。我还是会犯这样的错误。我讨厌vba。。谢谢。我的时间到了,我必须把这份工作表寄给我的教授;)太棒了!有时候,这需要正确的经验。对于那些只看到逗号分隔公式和参数的人来说,这一点并不明显。在使用
逗号作为小数分隔符的国家,这种情况常见吗?好的,是的。
Dim sep As String: sep = Application.International(xlListSeparator)
.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(COLUMN()" & sep & "2)"