【问题标题】:font color of data validation type cell数据验证类型单元格的字体颜色
【发布时间】:2021-03-18 08:51:36
【问题描述】:

我的 Excel 中有一个数据验证下拉菜单。如果有人将无效数据复制粘贴到这些下拉列表中,如果粘贴的值与下拉数据不匹配,我将使用条件格式以红色突出显示文本。 现在我正在尝试获取此下拉列表的文本颜色,以防使用 VBA 的数据不匹配。但我得到 -4105 作为颜色索引。

Function GetColorText(pRange As Range) As String

Dim xOut As Variant
Dim xValue As String
Dim i As Long
xValue = pRange.Text
MsgBox "cell value = " & xValue

xOut = pRange.DisplayFormat.fornt.Color
MsgBox "Color  = " & xOut

GetColorText = xOut
End Function

如果数据不匹配,有人可以帮助获取文本颜色

【问题讨论】:

  • 要检查条件格式设置的颜色,请使用pRange-DisplayFormat.Interior.Color。见stackoverflow.com/q/47419269/7599798
  • 这能回答你的问题吗? get conditional formatted color with vba in excel
  • 感谢您的快速响应。但这对我也不起作用。我在模块中编写的函数并在工作表中将此函数称为 excel 公式。
  • 不明白这与函数有何关系。顺便说一句:如果要检查文本颜色,请使用pRange.DisplayFormat.Font.Color
  • 请不要在 cmets 中发帖。编辑您的问题并将其放在那里。

标签: excel vba


【解决方案1】:

不支持在 UDF 中使用 DisplayFormat,请参阅 https://docs.microsoft.com/en-us/office/vba/api/excel.range.displayformat

有一个使用Evaluate 的解决方法,请参阅https://stackoverflow.com/a/54757688/7599798。在您的情况下,只需将 c.DisplayFormat.Interior.Color 替换为 c.DisplayFormat.Font.ColorIndex

Function GetColorText(pRange As Range) As String
    if isempty(pRange) exit function 
    Dim xOut As Variant
    xOut = pRange.Parent.Evaluate("DFColor(" & pRange.Address() & ")")
    GetColorText = xOut
    Debug.Print pRange.Address; xOut
End Function

Function DFColor(pRange As Range)
    DFColor = pRange.DisplayFormat.Font.ColorIndex
End Function

更新如果您不想在单元格为空的情况下返回任何内容,只需在例程顶部添加一个检查即可。查看更新的代码。

【讨论】:

  • 谢谢!!,我使用了相同的代码。如果文本以条件格式应用,它工作正常。但如果单元格是空的,它会返回相同的字体颜色(在我的情况下是红色。)。实际上它不应该为空单元格返回任何内容。如何克服这个问题。
  • 以下代码有时不会触发,在阅读了一些文章后,我在 UDF 中添加了“Application.volatile”语句。 UDF 仍然没有在 excel 单元格中调用,直到我得到并按 Enter 键。请对此提供帮助。
猜你喜欢
  • 2011-11-10
  • 1970-01-01
  • 2011-12-24
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 2011-10-23
相关资源
最近更新 更多