【问题标题】:Why isn't the right font color set in the smart table?为什么智能表中没有设置正确的字体颜色?
【发布时间】:2019-02-09 12:36:06
【问题描述】:

我在工作表上使用表格。 我选中“隐藏价格”框并运行相关代码。代码隐藏目标单元格中​​的值,将其中的字体颜色设置为填充颜色。 当您再次单击复选标记时,代码将再次启动并将单元格中的字体颜色设置为与左侧示例单元格中的相同。 由于使用了表格,用户可以选择一些表达样式,其中单元格中的字体颜色可以不同。因此,我不能设置任何特定的颜色,例如黑色。

With Sheets("Calculation")
    For r = 9 To 10
    For c = 22 To 23
        .Cells(r, c).Select
        clr1 = .Cells(r, c).DisplayFormat.Interior.ColorIndex
        clr2 = .Cells(r, c).DisplayFormat.Font.ColorIndex
        'colorindex of exemplary cell
        .Cells(r, 21).Select
        clr3 = .Cells(r, 20).DisplayFormat.Font.ColorIndex
        v = .Cells(2, 25).Value
        If .Cells(2, 25).Value = True Then
            If clr1 > 0 Then
                .Cells(r, c).Font.ColorIndex = clr1
            Else
                .Cells(r, c).Font.Color = 16777215
            End If
        Else
            .Cells(r, c).Font.ColorIndex = clr3
        End If
    Next c
    Next r

End With

由于某种原因,当我恢复字体颜色时,我设置了错误的颜色。 如果您查看示例单元格中字体的颜色: 单元格样式/创建单元格样式,然后颜色:Text1。 如果您查看目标单元格中​​的字体颜色: 单元格样式/创建单元格样式,然后颜色:米色 (rgb 128,128,0)。

我做错了什么?

更新。工作代码

Dim oSh As Worksheet
Dim rNg As Range
Set oSh = Sheets("Calculation")
Set rNg = oSh.Range("T_1_1[[Column22]:[Column23]]")
With oSh
    'bring in a variable cell format
    cLr = oSh.ListObjects("T_1_1").ListColumns(20).DataBodyRange.NumberFormat
    If .Cells(2, 25).Value = True Then
        'set "zero" formatting
        rNg.NumberFormat = ";;;"
    Else
        'apply formatting from sample
        rNg.NumberFormat = cLr
    End If
End With

【问题讨论】:

  • 我刚刚添加了一些示例代码来检查由表格样式或条件格式产生的 DisplayFormat 颜色。

标签: excel vba


【解决方案1】:

如何通过 NumberFormat 使单元格内容“不可见”;;;

要隐藏单元格的内容,我建议不要更改颜色。
而是存储原始 Range.NumberFormat 并将其设置为 Range.NumberFormat = ";;;" 隐藏正值、负值、空值和文本,即。 e.它隐藏了除错误之外的所有内容。

正常颜色和颜色索引

您始终可以读取或写入单元格的标准颜色,例如。 g.

  • Font.Color
  • Interior.Color

颜色值是一个3字节的RGB颜色,可以通过RGB(red byte, green byte, blue byte)设置并存储在Long变量中(4字节,代表0BGR颜色字节)。

ColorIndex 只是一个介于 1 和 56 之间的值 - 具有不同的结果,具体取决于工作簿的调色板。

按 DisplayFormat 显示的颜色

可以通过条件格式或表格样式更改显示的颜色。它不会更改标准颜色,而是覆盖它或可见,而不是基础标准颜色。
您只能读取叠加颜色,但不能直接为每个单元格设置

  • DisplayFormat.Font.Color
  • DisplayFormat.Interior.Color

如果更改底层.Font.Color,可见结果取决于覆盖的DisplayFormat的模式。

通过条件格式(FormatConditions)显示格式

如果要更改条件格式的DisplayFormat 颜色,则不能直接在单元格区域本身上设置它,而是在该范围的条件格式内设置。试验一下:

Private Sub DisplayColorByFormatCondition()
    Dim i As Long
    With ActiveSheet.Range("A1")
        Debug.Print "Color Info for " & .Cells.Address
        Debug.Print "Standard Font Color " & .Font.Color & _
                    " is displayed as " & .DisplayFormat.Font.Color; ""
        Debug.Print "Standard Interior Color " & .Interior.Color & _
                    " is displayed as " & .DisplayFormat.Interior.Color
        If .FormatConditions.Count = 0 Then
            Debug.Print "This cell is not part of a FormatCondition."
        Else
            For i = 1 To .FormatConditions.Count
                With .FormatConditions(i)
                    Debug.Print "Condition " & i & " sets Font Color to " & .Font.Color & _
                        "and Interior Color to " & .Interior.Color
                End With
            Next i
        End If
    End With
End Sub

按表格样式显示格式 (ListObject.TableStyle)

如果DisplayFormat 颜色来自表格样式,请尝试以下操作:

Private Sub DisplayColorByTableStyle()
    Dim lo As ListObject
    Dim i As Long
    With ActiveSheet.Range("A1")
        Debug.Print "Color Info for " & .Cells.Address
        Debug.Print "Standard Font Color " & .Font.Color & _
                    " is displayed as " & .DisplayFormat.Font.Color; ""
        Debug.Print "Standard Interior Color " & .Interior.Color & _
                    " is displayed as " & .DisplayFormat.Interior.Color
        For Each lo In ActiveSheet.ListObjects
            If Not Intersect(lo.Range, .Cells) Is Nothing Then
                Debug.Print "Cell is part of ListObject '" & lo.Name & _
                            "' which uses TableStyle '" & lo.TableStyle & "'"
                If Not Intersect(lo.HeaderRowRange, .Cells) Is Nothing Then
                    Debug.Print "Cell is part of HeaderRowRange. Font color is set to " & _
                                lo.HeaderRowRange.DisplayFormat.Font.Color & _
                                ", Interior color is set to " & _
                                lo.HeaderRowRange.DisplayFormat.Interior.Color
                ElseIf Not Intersect(lo.DataBodyRange, .Cells) Is Nothing Then
                    Debug.Print "Cell is part of DataBodyRange. Font color is set to " & _
                                lo.DataBodyRange.DisplayFormat.Font.Color & _
                                ", Interior color is set to " & _
                                lo.DataBodyRange.DisplayFormat.Interior.Color
                    For i = 1 To lo.ListRows.Count
                        If Not Intersect(lo.ListRows(i).Range, .Cells) Is Nothing Then
                            Debug.Print "Cell is part of ListRows(" & i & "). " & _
                                        "Font color is set to " & _
                                        lo.ListRows(i).Range.DisplayFormat.Font.Color & _
                                        ", Interior color is set to " & _
                                        lo.ListRows(i).Range.DisplayFormat.Interior.Color
                        End If
                    Next i
                End If
            End If
        Next lo
    End With
End Sub

【讨论】:

  • 是的,它有效!谢谢你的提示。遗憾的是无法弄清楚如何读取表格的格式。在应用的 express 样式中,第一行是绿色的,第二行是未上漆的。读取背景颜色和字体信息的属性是什么?
  • 感谢您的宝贵补充!现在很明显,在绿线中,内部颜色设置为 14348258。现在我了解了如何提取必要的数据。
猜你喜欢
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
相关资源
最近更新 更多