Pet*_*olm 6 excel rgb vba colors excel-vba
我想将单元格的字体颜色设置为特定的RGB值.
如果我使用
ActiveCell.Color = RGB(255,255,0)
Run Code Online (Sandbox Code Playgroud)
我得到黄色,但如果我使用更奇特的RGB值,如:
ActiveCell.Color = RGB(178, 150, 109)
Run Code Online (Sandbox Code Playgroud)
我只是回到灰色.
为什么我不能只使用任何RGB值?你知道任何变通方法吗?
谢谢.
Excel仅使用调色板中的颜色.使用RGB值设置单元格时,它会选择调色板中最接近匹配的单元格.您可以使用颜色更新调色板,然后选择颜色即可.
这将让您看到调色板中当前的内容:
Public Sub checkPalette()
Dim i As Integer, iRed As Integer, iGreen As Integer, iBlue As Integer
Dim lcolor As Long
For i = 1 To 56
lcolor = ActiveWorkbook.Colors(i)
iRed = lcolor Mod &H100 'get red component
lcolor = lcolor \ &H100 'divide
iGreen = lcolor Mod &H100 'get green component
lcolor = lcolor \ &H100 'divide
iBlue = lcolor Mod &H100 'get blue component
Debug.Print "Palette " & i & ": R=" & iRed & " B=" & iBlue & " G=" & iGreen
Next i
End Sub
Run Code Online (Sandbox Code Playgroud)
这将让您设置调色板
Public Sub setPalette(palIdx As Integer, r As Integer, g As Integer, b As Integer)
ActiveWorkbook.Colors(palIdx) = RGB(r, g, b)
End Sub
Run Code Online (Sandbox Code Playgroud)