【问题标题】:How to check the background color of an excel cell如何检查excel单元格的背景颜色
【发布时间】:2016-07-22 10:00:16
【问题描述】:

在我的程序中,我使用以下方法设置 excel 单元格的背景颜色:

sheet.cells(row, column).interior.color = System.Drawing.Color.Red

在我程序的另一部分,我想看看颜色是不是红色,但是这段代码:

If(sheet.cells(row, column).interior.color = System.Drawing.Color.Red) Then
    'Do something
End If

它返回一个“类型转换无效”异常。

如果颜色被检查:

If(sheet.cells(row, column).style.interior.color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red))
    'Do something
End If

颜色不相等(即使单元格是红色的),因为内部颜色是 16777215,颜色转换器返回 255。

如何正确比较颜色?

【问题讨论】:

  • 这是在您标记的 VB.NET 中还是在 VBA 中?
  • 看看这个链接是否可以帮助您将颜色转换为 RGB 格式以进行比较stackoverflow.com/questions/24132665/…
  • vb.net,它是用visual studio编写的插件,使用microsoft interrop library
  • 好的,解决了。通过使用单元格(x,y).style.Interior.color,我正在检查样式的内部颜色,而不是单元格本身的颜色。如果我需要删除这个问题,请告诉我,因为它是一个愚蠢的问题......
  • 使用 XLRgbColor 枚举代替 System.Drawing.Color msdn.microsoft.com/en-us/library/bb241561

标签: vb.net excel


【解决方案1】:

我不确定,但 System.Drawing.Color.xxx 在 vba 中不存在。你可以试试这个:

ActiveSheet.Cells(row, column).Interior.Color = RGB(255, 0, 0)

If (ActiveSheet.Cells(row, column).Interior.Color = RGB(255, 0, 0)) Then
    'Do something
End If

你也可以看看.Interior.ColorIndex 我认为您应该将问题的标签更改为 vba 而不是 vb.net。

最好的问候。

【讨论】:

  • 试过这个,不行。没有例外,但据说颜色不同。也就是说,这是一个 vb.net 插件,而不是 excel-vba,所以这可能会有所作为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
  • 2011-02-06
  • 2022-12-12
  • 2015-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多