Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 基于自动字体颜色的Excel求和_Vba_Excel_Colors - Fatal编程技术网

Vba 基于自动字体颜色的Excel求和

Vba 基于自动字体颜色的Excel求和,vba,excel,colors,Vba,Excel,Colors,我使用上述代码计算excel表格不同行中的黑色总和和红色总和,但正如您所知,字体选项中有一个自动黑色,当我使用该自动颜色(黑色)输入值时,它不会在自动颜色(黑色)下求和单元格值的总计变为红色总计,而不是黑色总计,我希望自动黑色总计应包含在黑色总计中 我正在使用 A11=colorsum(A1:A10,“黑色”) A11=colorsum(A1:A10,“红色”)很久以前,我编程了vba,但我认为颜色索引=0或xlNone或xlColorIndexAutomatic或xlColorIndexNon

我使用上述代码计算excel表格不同行中的黑色总和和红色总和,但正如您所知,字体选项中有一个自动黑色,当我使用该自动颜色(黑色)输入值时,它不会在自动颜色(黑色)下求和单元格值的总计变为红色总计,而不是黑色总计,我希望自动黑色总计应包含在黑色总计中

我正在使用
A11=colorsum(A1:A10,“黑色”)

A11=colorsum(A1:A10,“红色”)

很久以前,我编程了vba,但我认为颜色索引=0或xlNone或xlColorIndexAutomatic或xlColorIndexNone是自动的,黑色的颜色索引=1,这就是原因。您可以尝试使用上述建议值吗?

很久以前,我编程了vba,但我认为颜色索引为0或xlNone或xlColorIndexAutomatic或xlColorIndexNone是自动的,黑色的颜色索引为1,这就是原因。您可以尝试使用上述建议值吗?

使用
ColorIndex
作为参考可能很困难,因为您必须记住索引。我建议使用
color

 Public Function ColorSum(ByVal target As range, ByVal MyColor As String)
    Dim Blacksum As Long, Othersum As Long, cel As range
    Application.Volatile

    Blacksum = 0
    Othersum = 0
    For Each cel In target
        If IsNumeric(cel.Value) Then
            If cel.Font.ColorIndex = 1 Then
                Blacksum = Blacksum + cel.Value
            Else
                Othersum = Othersum + cel.Value
            End If
        End If
    Next cel

    ColorSum = IIf(LCase(MyColor) = "black", Blacksum, Othersum)

End Function
有两种方法可以使用公式,我希望代码是不言自明的:

必须注意的是,更新单元格颜色不会启动工作表以重新计算其公式。因此,每次更新单元格颜色时,必须手动按F9重新计算


使用
ColorIndex
作为参考可能很困难,因为您必须记住索引。我建议使用
color

 Public Function ColorSum(ByVal target As range, ByVal MyColor As String)
    Dim Blacksum As Long, Othersum As Long, cel As range
    Application.Volatile

    Blacksum = 0
    Othersum = 0
    For Each cel In target
        If IsNumeric(cel.Value) Then
            If cel.Font.ColorIndex = 1 Then
                Blacksum = Blacksum + cel.Value
            Else
                Othersum = Othersum + cel.Value
            End If
        End If
    Next cel

    ColorSum = IIf(LCase(MyColor) = "black", Blacksum, Othersum)

End Function
有两种方法可以使用公式,我希望代码是不言自明的:

必须注意的是,更新单元格颜色不会启动工作表以重新计算其公式。因此,每次更新单元格颜色时,必须手动按F9重新计算

xlColorIndexNone(和xlNone)是一个值为-4142的常数

xlColorIndexAutomatic(和xlAutomatic)是一个值为-4105的常量

使用Excel的GUI将单元格的颜色设置为“自动”通常会将ColorIndex设置为1,但如果设置之前是另一种颜色,则会将ColorIndex设置为-4105(即xlColorIndexAutomatic)

因此,我建议您分别检查1、xlColorIndexNone(或xlNone)和xlColorIndexAutomatic(或xlAutomatic)

换句话说,改变

Function SumByFontColor(MyRange As Range, Optional MyColor As Range)
    Dim Rng As Range
    Dim Col As Long

    Application.Volatile

    If MyColor Is Nothing Then
        Col = Application.Caller.Font.Color
    Else
        Col = MyColor(1).Font.Color
    End If

    SumByFontColor = 0
    For Each Rng In MyRange
        If Rng.Font.Color = Col Then
            SumByFontColor = SumByFontColor + Rng.Value2
        End If
    Next Rng
End Function

xlColorIndexNone(和xlNone)是一个值为-4142的常数

xlColorIndexAutomatic(和xlAutomatic)是一个值为-4105的常量

使用Excel的GUI将单元格的颜色设置为“自动”通常会将ColorIndex设置为1,但如果设置之前是另一种颜色,则会将ColorIndex设置为-4105(即xlColorIndexAutomatic)

因此,我建议您分别检查1、xlColorIndexNone(或xlNone)和xlColorIndexAutomatic(或xlAutomatic)

换句话说,改变

Function SumByFontColor(MyRange As Range, Optional MyColor As Range)
    Dim Rng As Range
    Dim Col As Long

    Application.Volatile

    If MyColor Is Nothing Then
        Col = Application.Caller.Font.Color
    Else
        Col = MyColor(1).Font.Color
    End If

    SumByFontColor = 0
    For Each Rng In MyRange
        If Rng.Font.Color = Col Then
            SumByFontColor = SumByFontColor + Rng.Value2
        End If
    Next Rng
End Function


你能告诉我这应该是什么代码吗?你能告诉我这应该是什么代码吗?如何获得特定单元格中红色字体单元格值的总和?当我使用时,它只对黑色值求和=sumbyfontcolor@JyotirmayaChandra只需使用要求和的颜色为单元格的字体着色。如何获得特定单元格中红色字体单元格值的总和?当我使用时,它只对黑色值求和=sumbyfontcolor@JyotirmayaChandra只需使用要求和的颜色为单元格的字体上色即可。