Setting colors in Hex and Decimal behaving differently

前端 未结 4 1224
攒了一身酷
攒了一身酷 2020-12-31 10:08

I am trying to set an orangish color in the following manner:

WorkSheet.Range(\"A1:A5\").Interior.color = 49407

and

WorkShe         


        
4条回答
  •  庸人自扰
    2020-12-31 10:39

    A Colorcode is usually composed of three values, RED, GREEN, BLUE. Your Hexcode is missing one of the three and excel is autofilling it with FF. So your color C0FF is transformed to be FFC0FF.

    Here is an Exampleprogram so you can see it in action, make a new sheet and execute it.

    ' C0FF will be changed to be FFC0FF
    Range("A1").Interior.Color = &HC0FF
    Range("A1").Select
    ActiveCell.FormulaR1C1 = Range("A1").Interior.Color
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "=DEC2HEX(R[-1]C,6)"
    
    ' 49407 is actually 00C0FF
    Range("B1").Interior.Color = 49407
    Range("B1").Select
    ActiveCell.FormulaR1C1 = Range("B1").Interior.Color
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "=DEC2HEX(R[-1]C,6)"
    
    ' Use RGB to have better control over your results
    ' Switch the order when doing so: 00C0FF => 00 Blue C0 Green FF Red
    Range("C1").Interior.Color = RGB(&HFF, &HC0, &H0)
    Range("C1").Select
    ActiveCell.FormulaR1C1 = Range("C1").Interior.Color
    Range("C2").Select
    ActiveCell.FormulaR1C1 = "=DEC2HEX(R[-1]C,6)"
    

    EDIT: Added a third example to illustrate switching the colors as suggested in a different answer.

提交回复
热议问题