conditional formatting get remove when I use vba code for highlighting rows

kamranyd

Board Regular
Joined
Apr 24, 2018
Messages
142
Office Version
  1. 2021
Platform
  1. Windows
VBA Code:
Private Sub worksheet_SelectionChange(ByVal Target As Range)

Dim RngRow As Range
Dim FndRng As Range
Dim Rng As Range
Dim RowRng As Range

'ActiveSheet.Unprotect

Selection.Worksheet.Cells.FormatConditions.Delete

Set FndRng = Intersect(Target, Range("A2:G1000"))

If Not FndRng Is Nothing Then
Set RngRow = Range("A2:G1000")

ActiveCell.FormatConditions.Add xlExpression, , True
ActiveCell.FormatConditions(1).Interior.Color = RGB(0, 0, 255)

Rows(ActiveCell.Row).FormatConditions.Add xlExpression, , "TRUE"
Rows(ActiveCell.Row).FormatConditions(2).Interior.Color = RGB(255, 0, 0)

End If

Set Rng = Intersect(Target, Range("H2:DW1000"))

If Not Rng Is Nothing Then
Set RowRng = Range("H2:DW1000")
   

ActiveCell.FormatConditions.Add xlExpression, , True
ActiveCell.FormatConditions(1).Interior.Color = RGB(128, 458, 128)
   
Rows(ActiveCell.Row).FormatConditions.Add xlExpression, , "TRUE"
Rows(ActiveCell.Row).FormatConditions(2).Interior.Color = RGB(255, 0, 0)

End If
End Sub

I set conditional formatting for finding duplicate values but these codes remove my conditional formatting, I wan these codes and also need conditional formatting.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,214,981
Messages
6,122,565
Members
449,089
Latest member
Motoracer88

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