Code not working as it should

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I was just looking for a basic code that when i select a cell it would turn Red.
When i leave that cell & select another it would revert back to its original colour & the new selected cell would then take on board the red colour.
What i am finding is this.
My whole sheet is yellow.
I select cell A1 & it then turns Red.
I then select cell B4.
Now cell A1 turns back to Yellow & now cell B4 is Red
So now looking at my sheet all cells are Yellow apart from the last selected cell of which in this case is B4
I save the sheet & close it.

Next time i open the sheet i see cell B4 is Red so i click in cell B22
Now i have cell B4 Red & also cell B22 Red

As i click around the code does what it should do but cell B4 is still Red.
I click cell Z1 save & close the worksheet.

Now when i open the worksheet i have cell Z1 & cell B4 Red so i start clicking around but these two cells dont change.
So every time i save & close the sheet the last selected cell will then be added each time to my growing Red cells.
Z1 B4 etc etc etc

Here is the code in use.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Static rngPrev As Range, PrevColor As Integer
    Dim TempColor As Integer
    TempColor = Target.Cells(1, 1).Interior.ColorIndex


    If Not rngPrev Is Nothing Then rngPrev.Interior.ColorIndex = PrevColor
    PrevColor = TempColor


    With Target.Interior
        .ColorIndex = 3
        .Pattern = xlSolid
    End With
    Set rngPrev = Target


End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Forum statistics

Threads
1,214,591
Messages
6,120,431
Members
448,961
Latest member
nzskater

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