Active Cell Highlighting in VBA

JBShandrew

Board Regular
Joined
Apr 17, 2011
Messages
54
Can some one please correct my incorrect syntax? I would like the cell that I am currently in to change the background color. This is what I have

.Range("C6:C15").ActiveCell.Interior.Color = RGB(255, 255, 0)

There are 9 cells, when I click in a cell or arrow down, or press the enter key to advance to the next cell in the range I would like it to be highlighted.

Thank you in advance,

Sincerely,

J.B.
 
<html><head><title>blockquote</title></head><BODY>This is what I have now. It is working<p>
thanks to the code you provided, I was able to make some minor changes<p>


<pre>

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'With Sheets("Sheet1")Not necessary
Dim rngcell As Range '1st time I used Dim
Select Case Target.Address ' I tried to use ("$B$6:$C$6") did not work.
' is there a way to use this with the case?
Case "$B$6", "$C$6", "$B$7", "$C$7", "$B$8", "$C$8", "$B$9", "$C$9", "$B$10", "$C$10", _
"$B$11", "$C$11", "$B$12", "$C$12", "$B$13", "$C$13", "$B$14", "$C$14", "$B$15", "$C$15", _
"$B$16", "$C$16", "$B$17", "$C$17", "$B$18", "$C$18", "$B$19", "$C$19", "$B$20", "$C$20", _
"$B$21", "$C$21", "$B$22", "$C$22", "$B$23", "$C$23", "$B$24", "$C$24", "$B$25", "$C$25", "$A$26"

For Each rngcell In Range("B6:C25")
Rem rngcell not defined
If rngcell.Address = Target.Address Then
rngcell.Interior.Color = RGB(255, 255, 0)
Else
rngcell.Interior.Color = RGB(0, 250, 250)
End If
Next rngcell
Case Else
Range("B6:C25").Interior.Color = RGB(245, 245, 245)
End Select

'End With Not necessary
End Sub


</pre></body></html>

</PRE>
 
Last edited:
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Since you're making your targeted area bigger, it may pay to make your code more extensible.




Thank you for the alternate way of

highlighting the active cell






Option Explicit

Private Sub Worksheet_Selectionchange(ByVal target As Range)

Dim rngChangeBackground As Range
Set rngChangeBackground = Range("B6:C25")


If Not Intersect(target, rngChangeBackground) Is Nothing Then 'i.e. target is at least partially within rngChangeBackground
rngChangeBackground.Interior.Color = RGB(0, 250, 250)
Intersect(target, rngChangeBackground).Interior.Color = RGB(255, 255, 0)
Else
rngChangeBackground.Interior.Color = xlNone 'target not within rngChangeBackground

End If
End Sub

</PRE>

 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,267
Members
452,902
Latest member
Knuddeluff

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