VBA to highlight cells based on multiple criteria

janu319

New Member
Joined
Feb 11, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hi,

I need to highlight cells in 5 columns based on the cell values, hope this can be done with VBA.

As shown in the attached image, the “Code” should be 1, 2, 3 or 4. For all other values, those cells needs to be highlighted in Yellow Color.

If the “Code” is 1 or 2, then the “Direction”, “Min”, “Max” and “Report” should be filled. Conditions are “Direction” must be one of E/W/N/S, if it is empty or filled with other value, then those cells needs to be highlighted in Yellow Color. “Min” and “Max” must be greater than 0(zero) but not more than 4.9. “Report” should not be empty.

If the “Code” is 3 or 4 or any other value, then the “Direction”, “Min”, “Max” and “Report” should be empty, if the cells are filled with any data, those needs to be highlighted. Min and Max can be 0 (zero).

Can we filter the rows which have at least one cell highlighted in Yellow?

I tried the conditional formatting but not much success.

Hope you can help me with this.

Thanks in advance.
 

Attachments

  • Sample.JPG
    Sample.JPG
    47.2 KB · Views: 37

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
See if this works
VBA Code:
Sub Test()

Dim Hide As Boolean
Dim cell As Range, rngCode As Range
Dim cellCol As Range, rngCol As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.ActiveSheet
Set rngCode = ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp))

For Each cell In rngCode
    Hide = True
    Set rngCol = ws.Range("D" & cell.Row, "G" & cell.Row)
    For Each cellCol In rngCol
        Select Case cell
            Case 1, 2
                Select Case ws.Cells(1, cellCol.Column)
                    Case "Direction"
                        If Not cellCol = "E" And Not cellCol = "W" And Not cellCol = "N" And Not cellCol = "S" Then
                            cellCol.Interior.ColorIndex = 6
                            Hide = False
                        End If
                    Case "Min", "Max"
                        If cellCol > 0 And Not cellCol > 4.9 Then
                            ' Do nothing
                        Else
                            cellCol.Interior.ColorIndex = 6
                            Hide = False
                        End If
                    Case "Report"
                        If cellCol = "" Then
                            cellCol.Interior.ColorIndex = 6
                            Hide = False
                        End If
                End Select
            Case Else
                If IsEmpty(cellCol) Then
                    cellCol.Interior.ColorIndex = 6
                    Hide = False
                End If
        End Select
    Next
    If Hide Then cell.EntireRow.Hidden = True
Next
    
End Sub
 
Upvote 0
Thank you Zot.
For "Code" 1 and 2 it is working perfect.
For "Codes" 3, 4 and other values, it is highlighting correct cells and not highlighting wrong cells.
If the "Code" is other than 1, 2, 3 and 4, then that cells needs to be highlighted.

1634124504005.png
 
Upvote 0
Thank you Zot.
For "Code" 1 and 2 it is working perfect.
For "Codes" 3, 4 and other values, it is highlighting correct cells and not highlighting wrong cells.
If the "Code" is other than 1, 2, 3 and 4, then that cells needs to be highlighted.

View attachment 48956
I could not understand what you were trying to indicate on your example above.

My understanding is that for Code 1, 2, 3, and 4, the highlight is correct. For code other than that you need the code to be highlighted too? Regardless whether the criteria are met, all cell in that row will need to be highlighted too?
 
Upvote 0
Sorry for the confusion.
Below matrix shows the possible values for Direction, Min, Max and Report for each code. If the values are not like this, then I want those cells to in highlighted in Yellow color.
Also, if the Code value it self is not one of 1, 2, 3 or 4, then I want those cells to be highlighted as well.

Hope it is clear.

Possible values
S.NoCodeDirectionMinMaxReport
11E/W/N/S0-4.90-4.9Fill
22E/W/N/S0-4.90-4.9Fill
33Empty0 or Empty0 or EmptyEmpty
44Empty0 or Empty0 or EmptyEmpty
5All other codesEmpty0 or Empty0 or EmptyEmpty
 
Upvote 0
Actually your instruction was clear in the very beginning but my brain was unclear o_O.
Try code below. I got the result like the one in your example. Should have check carefully before.
I use Hide to hide row without any highlight like before
VBA Code:
Sub Test()

Dim Hide As Boolean
Dim cell As Range, rngCode As Range
Dim cellCol As Range, rngCol As Range
Dim ws As Worksheet

Set ws = ActiveWorkbook.ActiveSheet
Set rngCode = ws.Range("C2", ws.Cells(Rows.Count, "C").End(xlUp))

For Each cell In rngCode
    Hide = True
    Set rngCol = ws.Range("C" & cell.Row, "G" & cell.Row)
    For Each cellCol In rngCol
        Select Case cell
            Case 1, 2
                Select Case ws.Cells(1, cellCol.Column)
                    Case "Direction"
                        If Not cellCol = "E" And Not cellCol = "W" And Not cellCol = "N" And Not cellCol = "S" Then
                            cellCol.Interior.ColorIndex = 6
                            Hide = False
                        End If
                    Case "Min", "Max"
                        If cellCol > 0 And Not cellCol > 4.9 Then
                            ' Do nothing
                        Else
                            cellCol.Interior.ColorIndex = 6
                            Hide = False
                        End If
                    Case "Report"
                        If IsEmpty(cellCol) Then
                            cellCol.Interior.ColorIndex = 6
                            Hide = False
                        End If
                End Select
            Case Else
                Select Case ws.Cells(1, cellCol.Column)
                    Case "Code"
                        If Not cellCol = 3 And Not cellCol = 4 Then
                            cellCol.Interior.ColorIndex = 6
                            Hide = False
                        End If
                    Case "Direction", "Report"
                        If Not cellCol = "" Then
                            cellCol.Interior.ColorIndex = 6
                            Hide = False
                        End If
                    Case "Min", "Max"
                        If Not cellCol = 0 And Not IsEmpty(cellCol) Then
                            cellCol.Interior.ColorIndex = 6
                            Hide = False
                        End If
                End Select
        End Select
    Next
    If Hide Then cell.EntireRow.Hidden = True
Next
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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