2011-10-20 28 views
-1

我正在動態創建Excel文件。保存爲XlFileFormat.xlXMLSpreadsheet格式時無法添加條件格式

我需要將文件另存爲XlFileFormat.xlXMLSpreadsheet。

但是,在將文件保存爲XlFileFormat.xlXMLSpreadsheet時,不會添加條件格式。

我打開文件&我檢查條件格式規則管理器並沒有添加任何內容。

我也沒有做任何文件保護。

With .Range("A1") 
    .FormatConditions.Delete() 
    .FormatConditions.Add(CInt(XlFormatConditionType.xlCellValue), _ 
     CInt(XlFormatConditionOperator.xlEqual), _ 
     "=" & """OK""").Interior.Color = CInt(eColor.FromArgb_100_51_204_51) 
    .FormatConditions.Add(CInt(XlFormatConditionType.xlCellValue), _ 
     CInt(XlFormatConditionOperator.xlNotEqual), _ 
     "=" & """OK""").Interior.Color = CInt(eColor.FromArgb_100_255_0_0) 
End With 

當我沒有將它保存爲XlFileFormat.xlXMLSpreadsheet格式時,條件格式工作正常。

任何想法?

+0

請使用代碼格式 –

回答

0

問題不在於SaveAs xml,而是您的條件格式代碼。

運行此代碼,將創建條件格式並使用xml文件進行保存。

Sub zx() 
    With [A1] 
     .FormatConditions.Delete 
     .FormatConditions.Add(xlCellValue, xlEqual, "OK").Interior.Color = RGB(51, 204, 51) 
     .FormatConditions.Add(xlCellValue, xlNotEqual, "OK").Interior.Color = RGB(255, 0, 0) 
    End With 

    ActiveWorkbook.SaveAs "C:\Users\Chris\Documents\Scratch\SOxx", xlXMLSpreadsheet 
End Sub 

我不確定eColor.FromArgb ...是什麼,所以代之以RGB調用。

請注意xlXMLSpreadsheet不支持vba,因此如果代碼位於活動工作簿中,它不會與SaveAs文件一起保存。

測試在Excel 2010中

+0

感謝,但是這個代碼不站在我這邊的工作,我想,當我保存到xlXMLSpreadsheet格式,格式化條件被忽略。如果我沒有指定任何格式,那麼它完美的作品 – someonewhowillnotbemiss