Setting Conditional Formatting based upon definitions table

CSweetwood

New Member
Joined
Dec 13, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm new to this forum as poster. In the past I was able to use the tips and solutions given here. Thank you for that!

Now I have an issue where I could not yet find the answer.
I have a sheet which needs conditional formatting. I want to do this with a table where I define all elements needed for setting up all condition rules. However, it sometimes give a "Subscript out of range" error when the 'applies to' does not include $C in the range.

1639402931676.png


I can't figure out why the range is not correct. If I set everything to C:C or C:D or B:C... (it includes C) it works....

VBA Code:
Sub Set_ConditionalFormatting()
'
'

Sort_ConditionalFormatting

On Error GoTo ErrorMessage


'Define Range
Dim MyRange As Range

'Delete Existing Conditional Formatting from entire sheet
Debug.Print "Clearing any existing conditional formatting on this sheet"
Sheets("Conditional Formatting").Cells.FormatConditions.Delete

    
    Dim rng As Range 'Range in which the conditions are defined
    Dim cell As Range 'for each line in the conditions table
    
    'Posstible conditions
    Dim Cond_Underline
    Dim Cond_Bold
    Dim Cond_Color
    Dim Cond_Italic
    Dim SetUnderline

    
    ' Walk through conditions table
    Debug.Print "Setting the conditions by going through the table"
    Set rng = Sheets("Conditional Formatting").Range("tbl_CondFormatting[Order]")
    RuleCount = 1
    For Each cell In rng
        
        Debug.Print "- Setting rule " & RuleCount
        RuleId = cell.Value2
        Rule = cell.Offset(0, 1).Value2
        Applies_To = cell.Offset(0, 2).Value2
        Cond_Bold = cell.Offset(0, 3).Value2
        Cond_Italic = cell.Offset(0, 4).Value2
        Cond_Underline = cell.Offset(0, 5).Value
        Cond_Color = cell.Offset(0, 6).Value2
        
        'Translate underline value into Excel property
        Select Case Cond_Underline    ' Evaluate Cond_Underline
        Case "None"     ' .
            'Debug.Print "None"
            SetUnderline = xlUnderlineStyleNone
        Case "Single"    '
            'Debug.Print "Single"
            SetUnderline = xlUnderlineStyleSingle
        Case "Double"    '
            'Debug.Print "Double"
            SetUnderline = xlUnderlineStyleDouble
            
        Case Else ' Other values.
            Debug.Print "Not sure what kind of underline is needed"
        End Select
                
        'Apply Conditional Formatting
        '-------------
        ' Define the Applies To field in the Conditional Formatting Rules Manager
          Set MyRange = Range(Applies_To)
        ' Define the Rule field in the Conditional Formatting Rules Manager
          MyRange.FormatConditions.Add Type:=xlExpression, Formula1:=Rule
        ' Define the formatting of the cell
          [B]With MyRange.FormatConditions(RuleId).Font '⬅️[/B]
            .Bold = Cond_Bold
            .Underline = SetUnderline
            .ColorIndex = Cond_Color
            .Italic = Cond_Italic
          End With
        'MyRange.FormatConditions(RuleId).StopIfTrue = False
        MyRange.FormatConditions(RuleId).StopIfTrue = True
        RuleCount = RuleCount + 1
    Next cell

Exit Sub
ErrorMessage:
Err.Number = vbObjectError + 1052
Debug.Print "ERROR while setting rule "; RuleCount
Debug.Print "Error: " & Err.Number & Chr(10) & "Description: " & Err.Description
Debug.Print "--------------------------------"
'MsgBox Err.Number

End Sub

Link to my work-in-progress Excel file.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi & welcome to MrExcel.
You cannot use RuleId when using different ranges. Try it like
VBA Code:
          With MyRange.FormatConditions(MyRange.FormatConditions.Count).Font
 
Upvote 0
Solution
Hi & welcome to MrExcel.
You cannot use RuleId when using different ranges. Try it like
VBA Code:
          With MyRange.FormatConditions(MyRange.FormatConditions.Count).Font
Swapping the line and it now works.
Simple change, great effect! Thanks, @Fluff !
 
Upvote 0
The complete VBA is now:

VBA Code:
Sub Set_ConditionalFormatting()
'
'

Sort_ConditionalFormatting

'On Error GoTo ErrorMessage


'Define Range
Dim MyRange As Range

'Delete Existing Conditional Formatting from entire sheet
Debug.Print "---------------------- START -----------------------------"
Debug.Print "Clearing any existing conditional formatting on this sheet"
Sheets("Conditional Formatting").Cells.FormatConditions.Delete

    
    Dim rng As Range 'Range in which the conditions are defined
    Dim cell As Range 'for each line in the conditions table
    
    'Posstible conditions
    Dim Cond_Underline
    Dim Cond_Bold
    Dim Cond_Color
    Dim Cond_Italic
    Dim SetUnderline

    
    ' Walk through conditions table
    Debug.Print "Setting the conditions by going through the table"
    Set rng = Sheets("Conditional Formatting").Range("tbl_CondFormatting[Order]")
    
    For Each cell In rng
        
        RuleId = cell.Value2
        Debug.Print "- Setting rule " & RuleId
        Rule = cell.Offset(0, 1).Value2
        Applies_To = cell.Offset(0, 2).Value2
        Cond_Bold = cell.Offset(0, 3).Value2
        Cond_Italic = cell.Offset(0, 4).Value2
        Cond_Underline = cell.Offset(0, 5).Value
        Cond_Color = cell.Offset(0, 6).Value2
        
        'Translate underline value into Excel property
        Select Case Cond_Underline    ' Evaluate Cond_Underline
        Case "None"     ' .
            'Debug.Print "None"
            SetUnderline = xlUnderlineStyleNone
        Case "Single"    '
            'Debug.Print "Single"
            SetUnderline = xlUnderlineStyleSingle
        Case "Double"    '
            'Debug.Print "Double"
            SetUnderline = xlUnderlineStyleDouble
            
        Case Else ' Other values.
            Debug.Print "Not sure what kind of underline is needed"
            SetUnderline = xlUnderlineStyleNone
        End Select
                
        'Apply Conditional Formatting
        '-------------
        ' Define the Applies To field in the Conditional Formatting Rules Manager
          Set MyRange = Range(Applies_To)
        ' Define the Rule field in the Conditional Formatting Rules Manager
          MyRange.FormatConditions.Add Type:=xlExpression, Formula1:=Rule
        ' Define the formatting of the cell
          With MyRange.FormatConditions(MyRange.FormatConditions.Count).Font
            .Bold = Cond_Bold
            .Underline = SetUnderline
            .ColorIndex = Cond_Color
            .Italic = Cond_Italic
          End With
        MyRange.FormatConditions(MyRange.FormatConditions.Count).StopIfTrue = False
        
    Next cell
Debug.Print "---------------------- END -------------------------------"
Exit Sub
ErrorMessage:
Err.Number = vbObjectError + 1052
Debug.Print "ERROR while setting rule "; RuleId
Debug.Print "Error: " & Err.Number & Chr(10) & "Description: " & Err.Description
Debug.Print "----------------------------------------------------------"


End Sub
Sub Sort_ConditionalFormatting()
'
' Sorting the table to add the rules in the defined order
'
    ActiveWorkbook.Worksheets("Conditional Formatting").ListObjects( _
        "tbl_CondFormatting").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Conditional Formatting").ListObjects( _
        "tbl_CondFormatting").Sort.SortFields.Add2 Key:=Range( _
        "tbl_CondFormatting[[#All],[Order]]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("Conditional Formatting").ListObjects( _
        "tbl_CondFormatting").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,212
Members
453,023
Latest member
alabaz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top