none
Change Excel Range Background Color RRS feed

  • Question

  • Hi All,

    I have created and applied a style-for background color, font, etc.- on a range/ cell using this article. Now I want to change the background color of that cell/ range but I don't want to modify the style. So I did this :

    range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(191, 191, 191));

    But it's background color is not changing, I even tried it with this code:

    range.Interior.ColorIndex = Excel.XlColorIndex.xlColorIndexNone;
    
    range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(191, 191, 191));
     

    Any idea!

    Thanks,


    Attiqe Ur Rehman

    Wednesday, February 26, 2014 7:21 AM

Answers

  • Hello Attiqe,

    Excel provides the Macro recorder for generating the code automatically. Did you try to record a macro for changing the color?

    Here is what I get in Excel VBA:

        Range("H8").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent5
            .TintAndShade = 0.399975585192419
            .PatternTintAndShade = 0
        End With
        Range("H8").Select

    It looks like setting the Color or ColorIndex properties is not enough. You need to set the Pattern property of the Interior class at least.

    You can read more about the Macro recorder in the Create or delete a macro article.

    Wednesday, February 26, 2014 3:55 PM

All replies

  • Hello Attiqe,

    Excel provides the Macro recorder for generating the code automatically. Did you try to record a macro for changing the color?

    Here is what I get in Excel VBA:

        Range("H8").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent5
            .TintAndShade = 0.399975585192419
            .PatternTintAndShade = 0
        End With
        Range("H8").Select

    It looks like setting the Color or ColorIndex properties is not enough. You need to set the Pattern property of the Interior class at least.

    You can read more about the Macro recorder in the Create or delete a macro article.

    Wednesday, February 26, 2014 3:55 PM
  • Sorry it was error in my code. :)

    Thanks,

    Attiqe


    Attiqe Ur Rehman

    Wednesday, February 26, 2014 4:42 PM