Avatar of toalexsandr
toalexsandr
Flag for United States of America asked on

Excel VBA Select Case issue

I am still in the beginer stage of VBA programming but a long time Excel user. I am in need of some assitance in writing a modified code. My task is to highlight rows in excel based on a keyword in a "T" column. Column "T" will have either Open, Closed, Recalled. I've tried using conditional formatting, but it wasnt working past first row. I have written a macro to do the highlighting and it works great. Here is the code...
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim oCell As Range
For Each oCell In Range("$T2:$T5000")
    Select Case oCell.Value
        Case Is = "Closed"
            oCell.EntireRow.Interior.Color = RGB(196, 215, 155)
        Case Is = "Recalled"
            oCell.EntireRow.Interior.Color = RGB(191, 191, 191)
        Case Is = "Open"
            oCell.EntireRow.Interior.ColorIndex = xlNone
    End Select
Next oCell
End Sub

Open in new window

But now I have a new challenge.  When I receive an document for review and its been reviewed, I record the Reviewed Date in Column "O" for that document.

Here is the problem.

Current macro just looks at Column"T" and highlights the rows.

While the document is in "Open" status, I need that row to be highlighted by a different color based on the cell not being empty in Column  "O" ( it will have a reviewed date). Afterwards, when Column "T" changes to either "Closed" or "Recalled", for that same document, the rows will be highlighted accordingly, regardless if the cell is empty or not empty in Column "O".
Microsoft ExcelProgrammingSpreadsheets

Avatar of undefined
Last Comment
toalexsandr

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
dlmille

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
dlmille

May I suggest a couple optimization steps?  Is there a need to look at every row in column T from row 2 to 5000, or just the row where a change has (or changes have) been made?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim oCell As Range

    If Not Intersect(Target, Range("T2:T5000")) Is Nothing Then
    
        For Each oCell In Target
        
                Select Case oCell.Value
                    Case Is = "Closed"
                        oCell.EntireRow.Interior.Color = RGB(196, 215, 155)
                    Case Is = "Recalled"
                        oCell.EntireRow.Interior.Color = RGB(191, 191, 191)
                    Case Is = "Open"
                        If Cells(oCell.Row, "O").Value = vbNullString Then
                            oCell.EntireRow.Interior.Color = vbYellow
                        Else
                            oCell.EntireRow.Interior.ColorIndex = xlNone
                        End If
                End Select
        Next oCell
        
    End If
End Sub

Open in new window


This code (above) checks only the rows that have been changed in column T (2:5000) and is triggered by Worksheet_Change() event, as opposed to Worksheet_SelectionChange() event, so should be much more efficient.

May I also ask what should be done if the value in column T were blank?  You don't seem to have code handling that, so you might be highlighting a row after the column T value were cleared, that didn't need highlighting.

Dave
toalexsandr

ASKER
Wow, this works perfect. Thanks Dave.
toalexsandr

ASKER
Yes, I dont mind optimizations.

I only need it to respond to when the status of the cell in column T changes.

You are right, I havent even considered when removing status in Column T to remove highlighting, and I just checked, and its not removing highlighting.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23