VBA Code to Highlight Rows Based on Multiple Criteria

AlwaysLearning2018

Board Regular
Joined
Nov 23, 2018
Messages
51
Hi All,

I am struggling with writing vba code to highlight rows in certain colors based on the values in a cell, sometimes values based on multiple cells. I have the below spreadsheet, in which I am attempting to highlight rows green where column J (PostSt) has a value of "Deleted" in it. I am trying to highlight rows orange where column J has a value of "Not Posted" in it. Last, I am trying to highlight rows Blue in which column N (ReviewSt) has a value of "Not Reviewed", but also where column J (BC) does not equal "SA" in any value. I have the below code to highlight rows for the criteria I noted above in green and orange, but can't seem to figure out how to now highlight the rows Blue where column N is "Not Reviewed" but also where column J does NOT equal "SA". I was wondering if anyone would be able to help me out and maybe get me pointed in the correct direction? My task is that I must create VBA code to accomplish what I am trying to do, even though I know I can achieve this without. Any help would be so greatly appreciated. Thank you!!

Dim LastRow As Long, c As Range
Dim MyRange As Range
LastRow = Cells(Cells.Rows.Count, "M").End(xlUp).Row
Set MyRange = Range("M1:M" & LastRow)
For Each c In MyRange
If (c.Value) = "Deleted" Then
c.EntireRow.Interior.Color = 5287936
ElseIf (c.Value) = "Not Posted" Then
c.EntireRow.Interior.Color = 49407
Else
c.EntireRow.Interior.Color = xlNone
End If
Next



[TABLE="width: 1067"]
<TBODY>[TR]
[TD]TD</SPAN>[/TD]
[TD] VD</SPAN>[/TD]
[TD] Port</SPAN>[/TD]
[TD] TN</SPAN>[/TD]
[TD] SecID</SPAN>[/TD]
[TD]Tdesc</SPAN>[/TD]
[TD] Units </SPAN>[/TD]
[TD] Price</SPAN>[/TD]
[TD] OFV</SPAN>[/TD]
[TD] BC</SPAN>[/TD]
[TD]MGC</SPAN>[/TD]
[TD]ApproveSt </SPAN>[/TD]
[TD] PostSt</SPAN>[/TD]
[TD]ReviewSt</SPAN>[/TD]
[/TR]
[TR]
[TD="align: right"]20190301</SPAN>[/TD]
[TD="align: right"] 20190409</SPAN>[/TD]
[TD="align: right"]123</SPAN>[/TD]
[TD="align: right"]22222</SPAN>[/TD]
[TD="align: right"]123456789 </SPAN>[/TD]
[TD]Matured</SPAN>[/TD]
[TD="align: right"]1000</SPAN>[/TD]
[TD="align: right"]100</SPAN>[/TD]
[TD="align: right"]0</SPAN>[/TD]
[TD] [/TD]
[TD][/TD]
[TD]Waiting </SPAN>[/TD]
[TD] Not Posted</SPAN>[/TD]
[TD]Not Reviewed</SPAN>[/TD]
[/TR]
[TR]
[TD="align: right"]20190301</SPAN>[/TD]
[TD="align: right"]20190409</SPAN>[/TD]
[TD="align: right"]456</SPAN>[/TD]
[TD="align: right"]555</SPAN>[/TD]
[TD="align: right"]987654321 </SPAN>[/TD]
[TD]Matured</SPAN>[/TD]
[TD="align: right"]3000</SPAN>[/TD]
[TD="align: right"]100</SPAN>[/TD]
[TD="align: right"]0</SPAN>[/TD]
[TD] [/TD]
[TD][/TD]
[TD]Waiting</SPAN>[/TD]
[TD]Deleted</SPAN>[/TD]
[TD]Not Reviewed</SPAN>[/TD]
[/TR]
[TR]
[TD="align: right"]20190409</SPAN>[/TD]
[TD="align: right"]20190409</SPAN>[/TD]
[TD="align: right"]1</SPAN>[/TD]
[TD="align: right"]888</SPAN>[/TD]
[TD="align: right"]194563278 </SPAN>[/TD]
[TD]Buy</SPAN>[/TD]
[TD="align: right"]1</SPAN>[/TD]
[TD="align: right"]0.01</SPAN>[/TD]
[TD="align: right"]0</SPAN>[/TD]
[TD] SA </SPAN>[/TD]
[TD]INV</SPAN>[/TD]
[TD]Pending</SPAN>[/TD]
[TD]Posted</SPAN>[/TD]
[TD]Not Reviewed</SPAN>[/TD]
[/TR]
[TR]
[TD="align: right"]20190409</SPAN>[/TD]
[TD="align: right"]20190409</SPAN>[/TD]
[TD="align: right"]2</SPAN>[/TD]
[TD="align: right"]999</SPAN>[/TD]
[TD="align: right"]986547230 </SPAN>[/TD]
[TD]Buy</SPAN>[/TD]
[TD="align: right"]1</SPAN>[/TD]
[TD="align: right"]0.01</SPAN>[/TD]
[TD="align: right"]0</SPAN>[/TD]
[TD] SA </SPAN>[/TD]
[TD]INV</SPAN>[/TD]
[TD]Pending</SPAN>[/TD]
[TD]Posted</SPAN>[/TD]
[TD]Not Reviewed</SPAN>[/TD]
[/TR]
</TBODY><COLGROUP><COL span=2><COL><COL><COL><COL><COL span=2><COL><COL><COL><COL><COL><COL></COLGROUP>[/TABLE]
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Is there some reason that it has to be VBA code?
If not, you should be able to do this with Conditional Formatting (no VBA required).
All things equal, that is usually the preferred way to go.

For example:
I am attempting to highlight rows green where column J (PostSt) has a value of "Deleted" in it.
Let's say that your data is in rows 2:100 (and your header row is in row 1).
Then do the following:
1. Select rows 2:100
2. Go to Conditional Formatting
3. Select the Formula option
4. Enter this formula: =$J2="Deleted"
5. Select the Green Fill format option
6. Click OK

This should highlight all rows with "Deleted" in column J green.

If you have more than one condition, just use AND (if all listed conditions must be met) or OR (if only one condition must be met).
That structure would look like:
=AND(condition1, condition2, ...)
 
Last edited:
Upvote 0
Hi Joe4,

Yes VBA is what I'm asked to create this with. Reason is this task is supposed to be setup basically as automatic as possible with little to no human intervention. This spreadsheet that I referenced I have already written several other vba codes for other conditions.
 
Upvote 0
Reason is this task is supposed to be setup basically as automatic as possible with little to no human intervention.
If that is really the case, Conditional Formatting is actually better, as it is automatic/dynamic.
VBA requires something to call it to run (either manually or based on events happening). So there are situations in which VBA may not run. But there are not any situations in which Conditional Formatting would not be applied!

If there is VBA running to populate or format the data "on-the-fly", then it would be better to have your VBA add the Conditional Formatting rules, instead of trying to create VBA code simulate Conditional Formatting.
You can get most of the VBA code you need to set up Conditional Formatting by turning on your Macro Recorder and record yourself performing the Conditional Formatting steps manually.
 
Upvote 0
Thanks for the explanation. I disagree with nothing you stated. The issue is The VBA code writing nonsense is a job request. I'll continue to figure it out on my own. Just thought someone on here may be able to give some guidance on the vba code aspect. Obviously definitely not anyone's task to give me free help.
 
Upvote 0
The issue is The VBA code writing nonsense is a job request. I'll continue to figure it out on my own.
You can still have VBA do it - just have VBA create the Conditional Formatting Rules like I suggested in the previous request. Using the Macro Recorder should give you most of the code you need without actually having to write it.

Otherwise, what you are doing is trying to have VBA emulate/simulate conditional formatting. Why do that when you can just tell VBA to do Conditional Formatting?
It would be like trying to create the addition function in VBA, instead of just telling VBA to use the existing addition function.

If I still haven't convinved you, and you really want to tell VBA to do it, without VBA setting up Conditional Formatting, let me know and I can show you how to do that.
I would just need to know one thing: How will this VBA be called to run? Do you want an event procedure to run it based on some event happening? Or will it be called manually or by another VBA procedure?
The answer to that impacts how we set it up.
 
Upvote 0
Hi Joe4

Thanks again. Can you please assist me with telling VBA to do so? The process will be initiated by going into the developer tab, selecting the macro I already have setup for either pieces of the process, and clicking run.
 
Upvote 0
Try this:
Code:
Sub MyMacro()

    Dim LastRow As Long
    Dim r As Long

    Application.ScreenUpdating = False

    LastRow = Cells(Cells.Rows.Count, "M").End(xlUp).Row
    
    For r = 1 To LastRow
        If Cells(r, "M") = "Deleted" Then
            Rows(r).Interior.Color = 5287936
        Else
            If Cells(r, "M") = "Not Posted" Then
                Rows(r).Interior.Color = 49407
            Else
                If (Cells(r, "N") = "Not Reviewed") And (Cells(r, "J") <> "SA") Then
                    Rows(r).Interior.Color = 12611584
                Else
                    Rows(r).Interior.Color = xlNone
                End If
            End If
        End If
    Next r
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,123
Members
452,381
Latest member
Nova88

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