Rash Amed
Rash Amed

Reputation: 697

Change the Background of MS Excel 2003

I'm a c# coder and so far this is the best way I know to express what I wanted. I am thinking of doing this code below on a microsoft excel 2003 worksheet. Could this be possible?

1st step

if(CellA1 == "Sunday" || CellA1== "Saturday"){
    // code to set background color of 
    // CellB1 to CellF1 to background color red
}

2nd step

// Execute this process from CellA1 until CellA10    
for(CellA1 up to CellA10){
    // do first step
}

So the code I wanted to do on MSExcel 2003 would look like this

for(int CellCount = 1;CellCount<10;CellCount++){

    if("CellA"+CellCount.ToString() == "Sunday" || 
       "CellA"+CellCount.ToString() == "Sunday"){

        // set ["CellB"+CellCount.ToString()].CellBackgroundColor="#FF0000";
        // set ["CellC"+CellCount.ToString()].CellBackgroundColor="#FF0000";
        // set ["CellD"+CellCount.ToString()].CellBackgroundColor="#FF0000";
        // set ["CellE"+CellCount.ToString()].CellBackgroundColor="#FF0000";
        // set ["CellF"+CellCount.ToString()].CellBackgroundColor="#FF0000";
    }

}

I'm not sure if this is possible. Or if there are any other ways equivalent on what I wanted to do, can you please help me. :) Thanks!

Upvotes: 1

Views: 389

Answers (2)

Nicola Cossu
Nicola Cossu

Reputation: 56357

Sub backcolor()
Range("a1:a10").Interior.ColorIndex = xlNone
For Each cell In Range("a1:a10")
    If cell.Value = "Sunday" Or cell.Value = "Saturday" Then
        cell.Interior.ColorIndex = 3
    End If
Next cell
End Sub

You can find the list of colors here

http://dmcritchie.mvps.org/excel/colors.htm

Upvotes: 2

chris neilsen
chris neilsen

Reputation: 53135

If all you want to do is change the color of a cell based on its value, just use conditional formatting in excel, no need for any programming.

Upvotes: 3

Related Questions