【发布时间】: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 颜色。