Creating colors with loop

时间:2015-10-30 21:19:32

标签: excel vba colors

I would like to create a maximum of twelve different colors by looping with incrementing color values. The problem is I am getting shades of the same color or too bright or too dark. I am using interior.color = xxxxxxx.

Any idea how to achieve some nice colors?

2 个答案:

答案 0 :(得分:1)

This'll make a nice rainbow :)

Sub colors()
    Application.ScreenUpdating = False
    r = 1
    c = 1
    rv = 50
    Do Until rv >= 255
        gv = 50
        r = 1
        Do Until gv >= 255
            bv = 50
            c = 1
            Do Until bv >= 255
                Cells(r, cc + c).Interior.Color = RGB(rv, gv, bv)
                'Print Nnumbers for color
                'Cells(r, cc + c).Value = rv & ", " & gv & ", " & bv
                bv = bv + 20
                c = c + 1
            Loop
            gv = gv + 20
            r = r + 1
        Loop
        rv = rv + 20
        cc = cc + 1
    Loop

    Do Until rv <= 0
        gv = 50
        r = 1
        Do Until gv >= 255
            bv = 50
            c = 1
            Do Until bv >= 255
                Cells(r, cc + c).Interior.Color = RGB(rv, gv, bv)
                'Print Nnumbers for color
                'Cells(r, cc + c).Value = rv & ", " & gv & ", " & bv
                bv = bv + 20
                c = c + 1
            Loop
            gv = gv + 20
            r = r + 1
        Loop
        rv = rv - 20
        cc = cc + 1
    Loop

End Sub

Disclaimer: this doesn't produce a full color wheel, just gets a good portion of the spectrum. For instance, it's missing the color pink.

答案 1 :(得分:1)

You can use .Interior.colorindex = i, i being an integer from 0 to 56 as per http://dmcritchie.mvps.org/excel/colors.htm. Regards,