Changing colour of selected cell

MrHibs

New Member
Joined
Sep 8, 2006
Messages
18
Hi guys,

Does anybody have a code that changes the colour of the cell you currently have selected?

So for example if you selected cell b3 that cell would now be green but if you selected b4, b3 would return to it's original colour and b4 would be highlighted green.

I'm also looking for a code that will do something similar but opposed to just changing the colour of the cell it will change the colour of the entire row that cell is in.

Cheers
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I forgot where I got this one,

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Cells.Interior.ColorIndex = -4142
With Target.Interior
.ColorIndex = 4
End With
End Sub

For the active row, select your range go to Format/Conditional formatting and using Formula Is,

=CELL("row")=ROW()

select a pattern, then enter this code

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
That works a treat thanks!

I don't suppose you know the code that changes the entire row as well?

Thanks again
 
Upvote 0
I forgot where I got this one,

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Cells.Interior.ColorIndex = -4142
With Target.Interior
.ColorIndex = 4
End With
End Sub

With regards to this one, it seems to set all the cells automatically to white (apart from the selected cell) which I'm guessing comes from:

Code:
Cells.Interior.ColorIndex = -4142

Is there a way that I can keep any colour formats I already have?
 
Upvote 0
Hi,

Try this code:
I thinks this will help u

Code:
Dim i As Integer
Dim name1 As String


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        if name1<> "" then
          Range(name1).Interior.ColorIndex = i
          i = ActiveCell.Interior.ColorIndex
        endif
    Target.Select
    With Selection.Interior
        .ColorIndex = 50
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
    End With

    name1 = Target.Address
End Sub
 
Upvote 0
I've encountered an issue with this one. It comes up with a runtime error if any of the cells on the page are protected.

Any suggestions?

Hi,

Try this code:
I thinks this will help u

Code:
Dim i As Integer
Dim name1 As String


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        if name1<> "" then
          Range(name1).Interior.ColorIndex = i
          i = ActiveCell.Interior.ColorIndex
        endif
    Target.Select
    With Selection.Interior
        .ColorIndex = 50
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
    End With

    name1 = Target.Address
End Sub
 
Upvote 0
hi,

If sheets are protected then try this code:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
   ActiveSheet.Unprotect
   If name1 <> "" Then
        Range(name1).Interior.ColorIndex = i
        i = ActiveCell.Interior.ColorIndex

   End If


    Target.Select
    With Selection.Interior
        .ColorIndex = 50
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
    End With

    name1 = Target.Address
    ActiveSheet.Protect
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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