2011-06-03 24 views
1

我是c#編碼器,迄今爲止,這是我知道表達我想要的最佳方式。 我正在考慮在Microsoft Excel 2003工作表上執行以下代碼。 這可能嗎?更改MS Excel 2003的背景

第一步

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

第二步

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

,所以我想在2003年msexcel的做的代碼應該是這樣的

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"; 
    } 

} 

我不知道這是否可能樂。或者如果在我想做的事情上有其他任何相同的方式,你能幫我一下。 :) 謝謝!

回答

2
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 

你可以找到的顏色列表在這裏

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

3

如果您只想根據其值更改單元格的顏色,只需在Excel中使用條件格式,無需任何編程。

+0

二是:http://office.microsoft.com/en-us/excel-help/creating-conditional-formatting-公式-HA001111661.aspx – Juliusz 2011-06-03 12:35:23

+0

它們可能正在讀取,但無法控制源文件 – datatoo 2011-06-03 13:49:30