FONT.COLOR在条件格式excel vba中返回NULL

时间:2013-08-23 15:30:51

标签: excel vba formatting conditional

我有一个子例程,它在表单“Source”中逐个单元格获取条件格式设置属性,并将它们应用于同一工作簿中的工作表“Result”中。它正常工作,当我得到Font.Color和Font.TintAndShade它返回null,所以在结果表中所有条件格式都有黑色字体颜色。 (您可以在结果表条件格式>管理规则中查看它)。

错误是“无效使用空”可能是因为值的类型。

此处出现错误:

FCFontC = Xarr(I, J).FormatConditions(II).Font.Color
FCFOntT = Xarr(I, J).FormatConditions(II).Font.TintAndShade

这是我的代码:

Sub FormatCondition1()
Dim FC As FormatCondition
Dim FCFontC As Long 
Dim FCFOntT As Integer
Dim FCC As Long
Dim FCT As Integer
Dim II As Integer
Dim Wks1 As Worksheet, Wks2 As Worksheet
Dim Xarr As Range, Yarr As Range
Dim I As Integer, J As Integer
'Dim K As Integer, L As Integer

Worksheets("Source").Activate
Worksheets("Result").Select

Set Yarr = Worksheets("Result").Range("A1:C2")
Yarr.Range("A1:C2").Clear

Set Wks1 = ActiveWorkbook.Worksheets("Source")
Set Wks2 = ActiveWorkbook.Worksheets("Result")

Set Xarr = Wks1.Range("A1:C2")
Set Yarr = Wks2.Range("A1:C2")

For I = 1 To Xarr.Rows.Count
    For J = 1 To Xarr.Columns.Count
        If Xarr(I, J).FormatConditions.Count > 0 Then
            For II = 1 To Xarr(I, J).FormatConditions.Count 'II number of conditions
                Set FC = Xarr(I, J).FormatConditions.Item(II)

                'cell font formatting
                '**** Error is HERE *******
                FCFontC = Xarr(I, J).FormatConditions(II).Font.Color
                FCFOntT = Xarr(I, J).FormatConditions(II).Font.TintAndShade

                'cell interior formating
                'Set FCCIn = Xarr.FormatConditions(II).Interior.PatternColorIndex
                 FCC = Xarr(I, J).FormatConditions(II).Interior.Color
                 FCT = Xarr(I, J).FormatConditions(II).Interior.TintAndShade

                'SET PROPERTY to NEW CELL
                Yarr(I, J).Select
                Selection.FormatConditions.Add Type:=xlCellValue, Operator:= _
                    FC.Operator, Formula1:=FC.Formula1
                'Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

                'cell font formatting
                Selection.FormatConditions(II).Font.Color = FCFontC
                Selection.FormatConditions(II).Font.TintAndShade = FCFOntT

                'cell interior formating
                With Selection.FormatConditions(II).Interior
                    .PatternColorIndex = xlAutomatic
                    .Color = FCC
                    .TintAndShade = FCT
                End With
                Selection.FormatConditions(Selection.FormatConditions.Count).StopIfTrue = False
            Next II
        End If
    Next J
Next I

End Sub

我的问题是“如何才能获得条件格式字体的属性?

0 个答案:

没有答案