highlighting cell with different color and rows and column with different color

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)

Selection.Worksheet.Cells.FormatConditions.Delete
Rows(ActiveCell.Row).FormatConditions.Add xlExpression, , "TRUE"
Rows(ActiveCell.Row).FormatConditions(1).Interior.Color = RGB(255, 128, 128)
Columns(ActiveCell.Column).FormatConditions.Add xlExpression, , "TRUE"
Columns(ActiveCell.Column).FormatConditions(2).Interior.Color = RGB(0, 204, 255)

End Sub

hello everyone. these codes work fine in showing row and columns highlighted when I select any cell. but how to give selected cell different color.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
How about
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Selection.Worksheet.Cells.FormatConditions.Delete
ActiveCell.FormatConditions.Add xlExpression, , True
ActiveCell.FormatConditions(1).Interior.Color = vbYellow
Rows(ActiveCell.Row).FormatConditions.Add xlExpression, , "TRUE"
Rows(ActiveCell.Row).FormatConditions(2).Interior.Color = RGB(255, 128, 128)
Columns(ActiveCell.Column).FormatConditions.Add xlExpression, , "TRUE"
Columns(ActiveCell.Column).FormatConditions(3).Interior.Color = RGB(0, 204, 255)
End Sub
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Selection.Worksheet.Cells.FormatConditions.Delete
ActiveCell.FormatConditions.Add xlExpression, , True
ActiveCell.FormatConditions(1).Interior.Color = vbYellow
Rows(ActiveCell.Row).FormatConditions.Add xlExpression, , "TRUE"
Rows(ActiveCell.Row).FormatConditions(2).Interior.Color = RGB(255, 128, 128)
Columns(ActiveCell.Column).FormatConditions.Add xlExpression, , "TRUE"
Columns(ActiveCell.Column).FormatConditions(3).Interior.Color = RGB(0, 204, 255)
End Sub
thank you very much really.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
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 did changes in these codes from my previous codes. but these codes give me error (run-time error'9':) saying "subscript out of range" why
 
Upvote 0
Which line gives the error?
 
Upvote 0
The only reason you would get a subscript out of range error on that line, is if the condition hadn't been added, but I can think of no reason why that would happen. Your code work quite happily for me.
 
Upvote 0
The only reason you would get a subscript out of range error on that line, is if the condition hadn't been added, but I can think of no reason why that would happen. Your code work quite happily for me.
these codes are in sheet2. when I copy data from sheet1 to sheet2 than this error get shown. otherwise codes are working fine if I don't copy data from sheet1.
 
Upvote 0
I cannot replicate behaviour.
 
Upvote 0

Forum statistics

Threads
1,215,227
Messages
6,123,743
Members
449,116
Latest member
alexlomt

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