【问题标题】:Excel: Set background color of cell and text color with rgbExcel:使用 rgb 设置单元格的背景颜色和文本颜色
【发布时间】:2017-03-26 18:37:26
【问题描述】:

我正在编写一个根据用户请求更改字体和背景颜色的程序。收到 backgroundColorDatatextColorData 后 由于用户的要求,我确实喜欢更改颜色,但我觉得有更好的方法来做到这一点然后我选择做什么(我的代码可能会重复自己) 我没有找到答案的另一个问题是如何使 textColor/backgroundColor 更“红色”或更“蓝色”

  Select Case backgroundColorData
        Case Is = "Black"
            Selection.Interior.Color = RGB(0, 0, 0)
        Case Is = "Red"
             Selection.Interior.Color = RGB(255, 0, 0)
        Case Is = "Blue"
             Selection.Interior.Color = RGB(0, 0, 255)
        Case Is = "White"
             Selection.Interior.Color = RGB(255, 255, 255)
  End Select


    Select Case textColorData
        Case Is = "Black"
            Selection.Font.Color = RGB(0, 0, 0)
        Case Is = "Red"
             Selection.Font.Color = RGB(255, 0, 0)
        Case Is = "Blue"
             Selection.Font.Color = RGB(0, 0, 255)
        Case Is = "White"
             Selection.Font.Color = RGB(255, 255, 255)
     End Select  

任何帮助将不胜感激。

【问题讨论】:

  • 欢迎来到 StackOverflow,大卫。虽然总是欢迎任何新用户,但在我看来,帖子可能放错了位置。如果您正在努力优化代码,那么您应该在此处发布您的问题:codereview.stackexchange.com 这是您发布仅需要优化的工作代码的地方。关于您的另一个问题:寻找比红色“更红”的颜色,您可能想尝试使用 windows 提供的颜色(16 + 百万种颜色),如果仍然不够,那么您可能想投资一台新显示器。
  • 嘿 @Ralph,感谢您的评论代码”。关于我的第二个问题,我想我没有很好地解释我的问题:当用户要求我使单元格背景更蓝或更红(等)时,相对于当前颜色,RGB 是否有任何选项可以做到这一点?例如这样的: .Font.Color = Rgb(+100 red) ??

标签: vba excel rgb background-color textcolor


【解决方案1】:
Sub tester()

    Dim backgroundColorData As String, textColorData As String

    backgroundColorData = "Blue"
    textColorData = "White"

    With Selection
        .Interior.Color = NameToRgb(backgroundColorData)
        .Font.Color = NameToRgb(textColorData)
    End With

End Sub

'map a color name to an rgb value
Function NameToRgb(sName As String) As Long
    Dim arrNames, arrRGB, v
    arrNames = Array("black", "red", "blue", "white")
    arrRGB = Array(RGB(0, 0, 0), RGB(255, 0, 0), _
                   RGB(0, 0, 255), RGB(255, 255, 255))

    v = Application.Match(LCase(sName), arrNames, 0)
    If Not IsError(v) Then
        NameToRgb = arrRGB(v - 1)
    Else
        NameToRgb = vbBlack 'default...
    End If
End Function

如果您想为“更红”的东西找到确切的颜色值,请将单元格中的背景设置为您想要的颜色,选择该单元格,然后在 VB 编辑器中输入即时窗格:

? Selection.Interior.Color 

复制数字并用它代替你的 RGB() 值

编辑:好的,现在我明白你说的让单元格更红是什么意思了...

Sub MoreRed(c As Range)
    Dim R As Long, G As Long, B As Long, clr As Long

    clr = c.Interior.Color
    B = clr \ 65536
    G = (clr - B * 65536) \ 256
    R = clr - B * 65536 - G * 256
    'Debug.Print R, G, B
    R = Application.Min(R + 20, 255) 'more red...
    c.Interior.Color = RGB(R, G, B)
End Sub

【讨论】:

  • 感谢@TimWilliams,这很有帮助!关于你答案的第二部分,当用户要求我让单元格背景更蓝或更红(等)时,也许我应该再问一次(更清楚)。相对于当前颜色,RGB 是否有任何选项可以做到这一点?例如这样的: .Font.Color = Rgb(+100 red) ??
  • 嗯,你当然可以尝试调整“红色”参数(RGB 调用中的第一个参数)。 RGB(100, 0, 255) 比 RGB(0, 0, 255) “更红”
  • 好的,但你的答案是假设当前单元格中的颜色为 RGB(0, 0, 255),我不知道,这就是我正在寻找的原因相对于颜色的方法已经存在......
  • 谢谢@Tim,我自己永远无法得到你的回答真的很有帮助,谢谢!
【解决方案2】:

欢迎堆栈溢出。

你可以用像这样的一个函数来做到这一点-

Function setColor(SelectionData As String) 

 Select Case SelectionData As String
      Dim returnValue As String  
        Case Is = "Black"
            returnValue  = RGB(0, 0, 0)
        Case Is = "Red"
             returnValue  = RGB(255, 0, 0)
        Case Is = "Blue"
             returnValue  = RGB(0, 0, 255)
        Case Is = "White"
             returnValue  = RGB(255, 255, 255)
     End Select  
return returnValue  
End Function

然后像这样调用你的函数-

setColor(textColorData)

【讨论】:

  • 谢谢,这也很有帮助!
猜你喜欢
  • 1970-01-01
  • 2011-02-06
  • 2015-10-21
  • 1970-01-01
  • 2017-05-02
  • 1970-01-01
  • 2015-01-01
  • 2010-11-21
  • 1970-01-01
相关资源
最近更新 更多