vba条件格式与标准偏差 – 错误的StDev运行时间错误

我试图将我的一些条件格式转移到VBA,但将其应用于每次运行macros时可能更改的范围。 我想我已经定义了我的范围,variables,公式和格式正确(从来没有做过vba format.conditions,所以语法可能是错误的)。 当我运行代码时,它停在线上

With checkrange.FormatConditions _ .Add(xlCellValue, xlGreater, Formula1:="=R" & cfcll.Row & "C" & q & "+ " & devone & ")") 

运行时错误5,无效的程序或调用。

代码的全部内容如下:

 Dim cflastrow As Long Dim cfrange As Range Dim cfcll As Range Dim checkrange As Range Dim q As Long Dim devone As Long Dim devtwo As Long Dim devthree As Long Dim devfour As Long cflastrow = finwb.Sheets("strt_Dash_Final").Cells(Rows.Count, 52).End(xlUp).Row Set cfrange = finwb.Sheets("Strt_Dash_Final").Range("AV6:AV" & cflastrow) For Each cfcll In cfrange If cfcll.value <> "" Then For q = 4 To 38 Set checkrange = finwb.Sheets("Strt_Dash_Final").Range(Cells((cfcll.Row + 1), q), Cells((cfcll.Row + (cfcll.value - 2)), q)) devone = Application.WorksheetFunction.StDev_P(checkrange) With checkrange.FormatConditions _ .Add(xlCellValue, xlGreater, Formula1:="=R" & cfcll.Row & "C" & q & "+ " & devone & ")") With .Font .ThemeColor = xlThemeColorDark1 .TintAndShade = 0 .ThemeFont = xlThemeFontMinor End With With .Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 255 End With End With With checkrange.FormatConditions _ .Add(xlCellValue, xlGreater, "=" & Cells(cfcll.Row, q).value & "+ 2*stddev(" & checkrange & ")") With .Font .Color = 255 .TintAndShade = 0 .ThemeFont = xlThemeFontMinor End With With .Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 49407 End With End With With checkrange.FormatConditions _ .Add(xlCellValue, xlGreater, "=" & Cells(cfcll.Row, q).value & "- stddev(" & checkrange & ")") With .Font .ThemeColor = xlThemeColorAccent3 .TintAndShade = -0.499984741 .ThemeFont = xlThemeFontMinor End With With .Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent3 End With End With With checkrange.FormatConditions _ .Add(xlCellValue, xlGreater, "=" & Cells(cfcll.Row, q).value & "- 2*stddev(" & checkrange & ")") With .Font .ThemeColor = xlThemeColorDark1 .TintAndShade = 0 .ThemeFont = xlThemeFontMinor End With With .Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 5287936 End With End With Next q ElseIf cfcll.value = "" Then 'nada End If Next cfcll 

此外,尽pipe范围'checkrange'肯定是正确的(检查购买使用checkrange = 5,所有值都去了5)devone总是出来为'1',当它应该是1.23 …..

我的理论是,我可能不会在公式上下文中正确使用R1C1,但是我发现它在其他一些示例中使用了这种方法,所以我现在还不确定。 任何帮助,一如既往,非常感谢!

 With checkrange.FormatConditions _ .Add(xlCellValue, xlGreater, Formula1:="=R" & cfcll.Row & "C" & q & "+ " & devone & ")") 

如果cfcll.Row是5并且q是4并且devone是1,则公式将会是=R5C4+ 1)

正如你所看到的,还有一个右括号。

 With checkrange.FormatConditions _ .Add(xlCellValue, xlGreater, Formula1:="=R" & cfcll.Row & "C" & q & "+ " & devone) 

如何debugging? 把公式放在一个stringvariables中

 sFormula = "=R" & cfcll.Row & "C" & q & "+ " & devone & ")" 

那么你会看到这一点。

对于具有非英语Excel版本的用户:使用VBA为FormatConditions设置的公式必须使用Excel的语言。 在VBA中,他们一定不能像美国英语一样。 所以R1C1将以德文Excel中的Z1S1为例。 这很奇怪和烦人。

对你的devone :它是一个整数types的Dim ed Long 。 所以不奇怪它不包含Double值。