Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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
Excel Font.ColorIndex在输入和输出之间不一致_Excel_Vba - Fatal编程技术网

Excel Font.ColorIndex在输入和输出之间不一致

Excel Font.ColorIndex在输入和输出之间不一致,excel,vba,Excel,Vba,我有以下函数,它解析一些格式设置并返回颜色索引: Function returnFontColor(targetString As String) As Integer Dim formatSheet As Worksheet Dim lastRow As Long Dim row As Long Dim counter As Integer returnFontColor = 0 Set formatSheet = ThisWorkbook

我有以下函数,它解析一些格式设置并返回颜色索引:

Function returnFontColor(targetString As String) As Integer

    Dim formatSheet As Worksheet
    Dim lastRow As Long
    Dim row As Long
    Dim counter As Integer

    returnFontColor = 0

    Set formatSheet = ThisWorkbook.Worksheets("Formatting")

    With formatSheet

        lastRow = .Cells(.Rows.Count, 2).End(xlUp).row

        For row = 2 To lastRow
            If LCase(CStr(.Range("B" & row).Value)) = LCase(CStr(targetString)) Then
                For counter = 1 To Len(.Range("C" & row).Value)
                    If .Range("C" & row).Characters(Start:=counter, Length:=1).Font.ColorIndex <> 0 Then

                        returnFontColor = .Range("C" & row).Characters(Start:=counter, Length:=1).Font.ColorIndex
                        GoTo Exiter
                    End If
                Next
            End If
        Next
    End With
Exiter:

End Function

我的问题是,调用此函数得到的颜色与格式化表的颜色明显不同,我不明白为什么,因为这应该依赖于非常具体的RGB。我缺少什么吗?

用户
Font.Color
而不是
Font.ColorIndex


“索引”返回可由多种颜色组成的托盘

返回颜色索引与返回颜色不同。颜色索引从调色板返回一个位置,而不是实际的RGB颜色

通常情况下,从主菜单的“颜色”下拉列表中选择单元格填充颜色。在这种情况下,两个具有不同Office主题的工作簿将显示不同的颜色,尽管颜色索引相同


要获取实际填充颜色,请使用
Font.color
方法并以双精度返回,因为可能的值为0到256^3。

您得到的值是什么?检查这里的颜色索引表:可能重复的我不知道这一点,似乎这确实是一个重复的问题。我似乎无法删除这个问题。
ws.Cells(row_num, col_num).Font.ColorIndex = returnFontColor(name)