gpt4 book ai didi

excel - 为什么智能表格中没有设置正确的字体颜色?

转载 作者:行者123 更新时间:2023-12-03 01:28:01 30 4
gpt4 key购买 nike

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

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

由于某种原因,当我恢复字体颜色时,我设置了错误的颜色。 enter image description here如果您查看示例单元格中字体的颜色:单元格样式/创建单元格样式,然后颜色: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

最佳答案

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

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

正常颜色和 ColorIndex

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

  • 字体颜色
  • 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

如果要更改条件格式的 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

关于excel - 为什么智能表格中没有设置正确的字体颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54606271/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com