Missed this one
To ThisWrokbook code moodule.
Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim cf As FormatCondition, r As Range
For Each cf In Sh.Cells.FormatConditions
If cf.Type = 2 Then
If cf.Formula1 Like "=ADDRESS(*" Then cf.Delete
End If
Next
For Each r In Target
r.FormatConditions.Add 2, Formula1:="=address(row(),column())=""" & r.Address & """"
r.FormatConditions(1).Interior.Color = vbYellow
r.FormatConditions(1).StopIfTrue = False
Next
End Sub
I would definitely
not use that code!
- If a large range is selected it causes my Excel to stop responding. After all, with say 5 columns selected there would be over 5,000,000 individual cell conditional formats to apply! (And then when something else was selected there would be over 5,000,000 individual cell conditional formats to delete)
- With my test sheets if I already had some existing conditional formatting applied, this code altered the colour of my existing CF to the yellow.
- The code results in the loss of the ability to use Excel's Undo function.
Point 1 above is actually not too much of a problem because the OP actually asked for the Active Cell to be highlighted not the Active Selection. So that code could be modified to just the Active Cell - but the issues in points 2 and 3 would still exist.
My suggestion is this, which overcomes all 3 of the above points.
A. Remove any codes that you have been trying in relation to this task from the ThisWorkbook module or individual sheet modules
B. For one of the relevant worksheets, select the whole worksheet (by clicking the box at the top left at the intersection of the column labels and row labels). We could select a lesser range but applying it to the whole worksheet doesn't seem to make files too big or slow in my experience.
C. Use Conditional Formatting -> New rule.. -> Use a formula to determine ... -> Format values where this formula is true:
=AND(CELL("col")=COLUMN(),CELL("row")=ROW()) -> Format... -> On the Fill tab select the colour you want
** for the Active cell -> Ok -> Ok
D. Repeat steps B and C for each worksheet where you might want the Active Cell highlighted. If there is a lot of sheets, a code could be written to do this as a once-off task.
E. Copy and Paste the code below into the ThisWorkbook module
F. Close the Visual Basic window & test.
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Application.ScreenUpdating = True
End Sub
** When choosing a colour, better to choose a colour that is not already used on the worksheet. On the Fill tab in the CF set-up if you go to 'More Colors...' it would be easy to make a colour unique for your sheet. With my method above, if it would help, you can use different colours on different worksheets for the Active Cell highlights. For example if you had multiple 'Income' type sheets you might want to use a shade of green and for 'Expense' type sheets you might want to use a yellow etc.