2017-02-06 126 views
0

如果任何單元格的條件格式爲true,我想檢查一個excel工作表。我有多種格式來格式化不同顏色的單元格,但我只想看看是否有任何條件是真的(不關心哪種情況)。這個例子不起作用,但我想知道我是否在正確的軌道上,甚至可能做我想問的問題。是否可以選擇條件格式爲true的所有單元格?

var lastCell = Globals.ThisAddIn.Application.ActiveCell.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing); 
var firstCell = ((Worksheet)Globals.ThisAddIn.Application.ActiveSheet).Cells[2,1]; 
var range = ((Worksheet)Globals.ThisAddIn.Application.ActiveSheet).Range[firstCell, lastCell] 
var r = range.SpecialCells(XlCellType.xlCellTypeAllFormatConditions, true); 
+0

循環遍歷單元格並將它們添加到列表中,如果它們匹配的條件?如果可能,或者使用linq – EpicKip

回答

0

由於FormatConditions收集了一系列適用於整個範圍,我不相信有一種機制來確定範圍內的每個單元格的值。我認爲你最好的選擇是在感興趣的單元格中測試Formula我相信這不是你想聽到的

獲取使用範圍this answer。當然,用((Worksheet)Globals.ThisAddIn.Application.ActiveSheet)代替xlWorkSheet

然後,假設你把它設置爲range,通過一系列環......

for (int r = 1; r <= range.Rows.Count; r++) 
{ 
    for (int c = 1; c <= range.Columns.Count; c++) 
    { 

...暫時的東西,如(前言更換公式/值:我幾乎保證這個代碼將不會工作)...

 string formula = range[r, c].Formula; //or Value, check HasFormula 
     for (int i = 1; i <= range.FormatConditions.Count; i++) 
     { 
      range[r, c].Formula = range.FormatConditions[i].Formula1; // or Formula2 (?) 
     } 
     bool condition = range[r, c].Value; 
     // do work based on the condition 

     range[r, c].Formula = formula; 

...並關閉循環。

} 
} 
相關問題