vba 具有标准偏差的条件格式 - 运行 时间错误与不正确的 StDev

vba conditional formatting with standard deviations - run time error with incorrect StDev

我正在尝试将一些条件格式移动到 VBA,但将其应用于每次 运行 宏时可能会更改的范围。我想我已经正确定义了我的范围、变量、公式和格式(从来没有做过 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

此外,尽管范围 '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)

如您所见,much 有一个右括号。

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

如何调试?先把公式放在字符串变量中

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

那你就看到了。

对于非英语 Excel 版本的用户: 为 FormatConditions 和 VBA 设置的公式必须使用 Excel 的语言。在 VBA 中,它们不能像往常一样使用美式英语。例如,R1C1 在德语 Excel 中将是 Z1S1。那很奇怪也很烦人。

还有你的devone:它被Dim编辑为Long,这是一个整数类型。所以它不包含 Double 值也就不足为奇了。