Consulting

Results 1 to 3 of 3

Thread: how to define color of the cell by coding

  1. #1

    how to define color of the cell by coding

    I want to change the color of my cells based on the value. how can I write it in VBA?
    If worksheets("sheet1").cells(1,i).value=1 then
    worksheets("sheet1").cells(1,i).color=blue


    how to write the bold line?

  2. #2
    Hi Mary

    Three options for you:

    1.)
    worksheets("sheet1").cells(1,i).interior.colorindex = 5

    When you record a macro that changes cell color to something, you get a code that changes colorindex. Each color has a colorindex, that you can determine by recording a macro that changes a cell's color as desired. You can then use this colorindex with method #1 above.

    2.)
    worksheets("sheet1").cells(1,i).interior.color=vbBlue

    With method #2 you use built-in constants, which are not too many, a dozen or so, AFAIK, but they are easily recognizable (vbBlue, vbBlack, vbRed, etc).

    3.)
    worksheets("sheet1").cells(1,i).interior.color = RGB(0, 0, 255)

    With RGB function, you can blend any color you want by varying the amount of Red, Green and Blue components in the blend.

    HTH

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by JimmyTheHand
    3.)
    worksheets("sheet1").cells(1,i).interior.color = RGB(0, 0, 255)

    With RGB function, you can blend any color you want by varying the amount of Red, Green and Blue components in the blend.
    Not correct. Excel will map it to the closest match of colours on the colour palette. You have 56 in pre-2007 Excel, that's it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •