Редактировать:
Вы можете просто преобразовать длинное значение из ячейки в соответствующий Excel Enum
, используя несколько пользовательских функций.
Sub s_ApplyFormats()
Dim rng_FormattingInfo As Range
Dim rng_RowItem As Range
Dim i As Long 'used in loop for row numbers
Dim VBAFormatTypeCol As Excel.XlFormatConditionType, VBAOperatorCol As Excel.XlFormatConditionOperator
Set rng_FormattingInfo = Range("rng_ConditionalFormattingData")
For Each rng_RowItem In rng_FormattingInfo.Rows
i = i + 1
With Worksheets(rng_RowItem.Cells(i, int_WorksheetCol).Value2)
VBAFormatTypeCol = FormatTypeToXLEnum(CLng(Trim(rng_RowItem.Cells(i, int_VBAFormatTypeCol).Value2)))
VBAOperatorCol = OperatorTypeToXLEnum(CLng(Trim(rng_RowItem.Cells(i, int_VBAOperatorCol).Value2)))
With .Range(rng_RowItem.Cells(i, int_RangeCol))
.FormatConditions.Add(VBAFormatTypeCol, VBAOperatorCol, rng_RowItem.Cells(i, int_FormulaCol))
End With
.Font.ColorIndex = rng_RowItem.Cells(i, int_FormatCol).Font.ColorIndex
.Interior.ColorIndex = rng_RowItem.Cells(i, int_FormatCol).Interior.ColorIndex
End With
VBAFormatTypeCol = Empty
VBAOperatorCol = Empty
Next rng_RowItem
End Sub
Private Function FormatTypeToXLEnum(ByVal FormatType as Long) As Excel.XlFormatConditionType
Select Case FormatType
Case 1
FormatTypeToXLEnum = xlCellValue
Case 2
FormatTypeToXLEnum = xlExpression
End Select
End Function
Private Function OperatorTypeToXLEnum(ByVal OperatorType as Long) As Excel.XlFormatConditionOperator
Select Case OperatorType
Case 1
OperatorTypeToXLEnum = xlBetween
Case 2
OperatorTypeToXLEnum = xlNotBetween
Case 3
OperatorTypeToXLEnum = xlEqual
Case 4
OperatorTypeToXLEnum = xlNotEqual
Case 5
OperatorTypeToXLEnum = xlGreater
Case 6
OperatorTypeToXLEnum = xlLess
Case 7
OperatorTypeToXLEnum = xlGreaterEqual
Case 8
OperatorTypeToXLEnum = xlLessEqual
End Select
End Function
Оригинал:
Попробуйте явно преобразовать значения ячеек в тип long. Подумайте о следующем:
'Define constants at the top of the module
Private Const int_VBAFormatTypeCol As Long = 2 'xlCellValue or xlExpression
Private Const int_VBAOperatorCol As Long = 4 'xlEqual, xlBetween, etc.
Private Const int_FormulaCol As Long = 5
Private Const int_WorksheetCol As Long = 6
Private Const int_RangeCol As Long = 7
Private Const int_FormatCol As Long = 8
Private Const int_StopIfTrueCol As Long = 10
Sub s_ApplyFormats()
Dim rng_FormattingInfo As Range
Dim rng_RowItem As Range
Dim i As Long 'used in loop for row numbers
Dim VBAFormatTypeCol As Long, VBAOperatorCol As Long
Set rng_FormattingInfo = Range("rng_ConditionalFormattingData")
For Each rng_RowItem In rng_FormattingInfo.Rows
i = i + 1
With Worksheets(rng_RowItem.Cells(i, int_WorksheetCol).Value2)
VBAFormatTypeCol = CLng(Trim(rng_RowItem.Cells(i, int_VBAFormatTypeCol).Value2))
VBAOperatorCol = CLng(Trim(rng_RowItem.Cells(i, int_VBAOperatorCol).Value2))
With .Range(rng_RowItem.Cells(i, int_RangeCol))
.FormatConditions.Add(VBAFormatTypeCol, VBAOperatorCol, rng_RowItem.Cells(i, int_FormulaCol))
End With
.Font.ColorIndex = rng_RowItem.Cells(i, int_FormatCol).Font.ColorIndex
.Interior.ColorIndex = rng_RowItem.Cells(i, int_FormatCol).Interior.ColorIndex
End With
VBAFormatTypeCol = Empty
VBAOperatorCol = Empty
Next rng_RowItem
End Sub
Попробуйте запустить его и посмотреть, что вы получите.