0
votes

I am trying to replace all of the conditional formatting of a spreadsheet (over 30 formatting rules). I have created a class module (called ConditionalFormatting) that has a series of subs for all the formatting rules, with one sub for each range that needs conditional formatting. The only way I have thought to do this (open to suggestions) is by having the worksheet_change event call a sub in the ConditionalFormatting class called FormattingSubs which will call the correct sub to perform the formatting.

Here is the code for FormattingSubs:

Public Sub FormattingSubs(target As Range)
    'have logic here to call the right sub based on what target.address
    'is from the worksheet.change event

    Select Case target.Name.Name
        Case "head_pouch_lot_number"
            Call HeadPouchLotNumber(target)
        Case "head_consumed_pouch_lot"
            Call HeadConsumedPouchLot(target)
        Case "section_one_heading"
            Call SectionOneHeading
    End Select

End Sub

And here is the code for one of the formatting subs, HeadConsumedPouchLot: (note that the color variables are public constants defined in a separate module)

Public Sub HeadConsumedPouchLot(target As Range)
    Dim head_consumed_pouch_lot As Range
    Dim ws As Worksheet
    Set head_consumed_pouch_lot = ActiveSheet.Range("head_consumed_pouch_lot")
    Set ws = target.Worksheet

    If target.address <> head_consumed_pouch_lot.address Then
        Set target = head_consumed_pouch_lot
    End If

    With ws.Range(target.address)
        If Range("section_one_heading").Value <> "" Then
            .Interior.ColorIndex = red
            .Font.ColorIndex = yellow
        Else
            .Interior.ColorIndex = lightgreen
            .Font.ColorIndex = black
        End If
    End With

The problem is that when it goes to actually set the color, it gives me the 1004 error: "Application-defined or object-defined error."

What is wrong with my code?

1
What worksheet is target on? - Rusan Kax
Which line does the error actually occurs? - PatricK
If "head_consumed_pouch_lot" is a Named Range in the workbook, you can use ThisWorkbook.Names("head_consumed_pouch_lot").RefersToRange instead of ActiveSheet - PatricK
@RusanKax As I am calling the FormattingSubs routine from the worksheet change event, target is on the worksheet the changed cell is on. In this case, "head_consumed_pouch_lot" is on Sheet 1, named "FA-201C." - deasa
@PatricK The code errors on the .Interior.ColorIndex = <color>. It will error out under the If or the Else. - deasa

1 Answers

4
votes

The problem I figured out was that I needed to unprotect my sheet before I could make any changes to it! Thank you all for helping me look for answers.