1
可選屬性

我已經寫了兩VBA子程序:錯誤,同時檢查條件格式的VBA

1)要設置條件格式(與操作者,一級方程式和formula2可選的)

Sub setConditionalFormatting(sheetName As String, cellRange As String, CFcellColor As String, CFfontColor As String, CFtype As XlFormatConditionType, Optional CFoperator As Variant, Optional CFformula1 As Variant, Optional CFformula2 As Variant) 
On Error GoTo Errhandler 
    Dim sheet As Worksheet 
    Dim cell As range 
    Set sheet = Sheets(sheetName) 
    sheet.Select 
    Set cell = range(cellRange) 
    cell.Select 
    'user defined sub to print string in a file 
    Call OutputString("Setting Conditional Formatting...") 

    With cell.FormatConditions.Add(_ 
     Type:=CFtype, _ 
     Operator:=CFoperator, _ 
     Formula1:=CFformula1, _ 
     Formula2:=CFformula2) 

     .Interior.color = CFcellColor 
     .Font.color = CFfontColor 
    End With 

    Call OutputString("Conditional Formatting successfully applied") 
Exit Sub 
Errhandler: 
    'a sub for error handling task 
    Call ErrorHandler(Err) 
    Exit Sub 
End Sub 

2 )要通過片檢查條件格式(CF)和每個CF打印屬性:

Sub checkConditionalFormattingsOnSheet(sheetName As String, rng As String) 
On Error GoTo Errhandler 
    Dim cellRange As range 
    Dim i As Integer 
    Dim temp As Variant 
    Sheets(sheetName).Select 

    Set cellRange = range(rng) 
    cellRange.Select 

    If cellRange.FormatConditions.Count > 0 Then 
     Call OutputString("Conditional formatting (CF) in sheet " + sheetName + ":") 
     For i = 1 To cellRange.FormatConditions.Count 
      Call OutputString(CStr(i) + ") Conditional Formatting-") 
      Call OutputString("Interior Color: " + CStr(cellRange.FormatConditions(i).Interior.color)) 
      Call OutputString("Font Color: " + CStr(cellRange.FormatConditions(i).Font.color)) 
      Call OutputString("CF Type: " + CStr(cellRange.FormatConditions(i).Type)) 

      If IsMissing(cellRange.FormatConditions(i).Operator) Then 
       Call OutputString("CF Operator: Not Applicable") 
      Else 
       Call OutputString("CF Operator: " + CStr(cellRange.FormatConditions(i).Operator)) 
      End If 

      Call OutputString("Formula1: " + CStr(cellRange.FormatConditions(i).Formula1)) 

      If IsMissing(cellRange.FormatConditions(i).Formula2) Then 
       Call OutputString("CF Formula2: Not Applicable") 
      Else 
       Call OutputString("Formula2: " + CStr(cellRange.FormatConditions(i).Formula2)) 
      End If 
     Next i 
    ElseIf cellRange.FormatConditions.Count = 0 Then 
     Call OutputString("No conditional formatting found in sheet " + sheetName) 
    End If 

Exit Sub 
Errhandler: 
    Call ErrorHandler(Err) 
    Exit Sub 
End Sub 

現在,當我想設置條件格式,說:「與價值馬麗娟細胞r除2應具有細胞在RGB(198,239,206)的着色和字體作爲RGB(255,255,0)」通過使函數調用

'PS: I am not parameterizing Optional value- Formula2 here 
Call setConditionalFormatting("MyWrkSheet", "C5:N13", RGB(198, 239, 206), RGB(255, 255, 0), xlCellValue, xlGreater, "=2") 

我在If IsMissing(cellRange.FormatConditions(i).Formula2)在checkConditionalFormattingsOnSheet得到一個錯誤:

錯誤:應用程序定義或對象定義的錯誤 HelpContext:1000095,ErrorID中:1004個

我曾嘗試其他選項,例如 '是Nothing', 'ISNULL()' 和傳遞參數爲Formula2分別爲Nothing和Null,但沒有 運氣好的話!

感謝您的時間和耐心提前! :)

+0

PS:我知道,一個選擇是使用默認值,但我正在尋找其他的選擇,因爲它不使用默認值作爲參數的非常好的做法。 – Rishi

+1

如果你嘗試檢查'Formula2'時,你應該得到1004錯誤,而不是91錯誤。 – Rory

+0

是的,你是對的@Rory,正如你指出的那樣,它確實是1004錯誤而不是91錯誤。那91錯誤應該是由於我在調試時意外添加了無效的行。現在,我有一種感覺,你對這個線程的下一個評論會解決我的問題:) – Rishi

回答

1

根據MS Documentation.Formula2是「僅當數據驗證條件格式運算符屬性是xlBetween或xlNotBetween時使用」。

因此,我想,你應該在嘗試訪問.Formula2之前檢查.Operator屬性。

喜歡的東西...

If cellRange.FormatConditions(i).Operator = xlBetween or celRange.FormatConditions(i).Operator = xlNotBetween Then 
    If IsMissing(cellRange.FormatConditions(i).Formula2) Then 
     Call OutputString("CF Formula2: Not Applicable") 
    Else 
     Call OutputString("Formula2: " + CStr(cellRange.FormatConditions(i).Formula2)) 
    End If 
End If 
+0

對不起,我沒有在這裏更新我最後做過的事情。 Btws,是的,你是對的。事實上,我已經使用了相同的條件,只有這樣我纔會打印Formula2。實際上,我也在檢查我的formatcondition(i).type是xlexpression還是xlCellValue,因此我檢查了formatconditions(i).Operator。 – Rishi