VBA for highlighting row and column of active cell

SRMPURCHASE

Board Regular
Joined
Dec 23, 2014
Messages
206
Office Version
  1. 2016
Platform
  1. Windows
I would like to modify the following VBA to only highlight to the left and up from the active cell, can anyone help?
I like this solution as it involves no conditional formatting to perform.

Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static rr
Static cr

If cc <> "" Then
With Columns(cc).Interior
.ColorIndex = xlNone
End With
With Rows(rr).Interior
.ColorIndex = xlNone
End With
End If

rr = Selection.Row
cc = Selection.Column

With Columns(cc).Interior
.ColorIndex = 8
.Pattern = xlSolid
End With
With Rows(rr).Interior
.ColorIndex = 8
.Pattern = xlSolid
End With
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,456
Office Version
  1. 365
Platform
  1. Windows
Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

Static rng As Range


Application.ScreenUpdating = False


If Target.Areas.Count <> 1 Then Exit Sub


If Not rng Is Nothing Then rng.Interior.ColorIndex = xlNone


Set rng = Union(Target(1).Resize(Rows.Count - Target(1).Row), _
    Target(1).Resize(, Columns.Count - Target(1).Column))
 
With rng.Interior
    .ColorIndex = 8
    .Pattern = xlSolid
End With


End Sub
 
Upvote 0

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,456
Office Version
  1. 365
Platform
  1. Windows
The previous code I posted highlights to the right and down.
The following highlights to the left and up :-
Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static rng As Range


Application.ScreenUpdating = False


If Target.Areas.Count <> 1 Then Exit Sub


If Not rng Is Nothing Then rng.Interior.ColorIndex = xlNone


Set rng = Union(Range(Cells(Target(1).Row, 1), Target(1)), _
    Range(Cells(1, Target(1).Column), Target(1)))
 
With rng.Interior
    .ColorIndex = 8
    .Pattern = xlSolid
End With


End Sub
 
Upvote 0

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,456
Office Version
  1. 365
Platform
  1. Windows
Or perhaps this :-
Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static rng As Range


Application.ScreenUpdating = False


If Not rng Is Nothing Then rng.Interior.ColorIndex = xlNone


Set rng = Union(Range(Cells(ActiveCell.Row, 1), ActiveCell), _
    Range(Cells(1, ActiveCell.Column), ActiveCell))
 
With rng.Interior
    .ColorIndex = 8
    .Pattern = xlSolid
End With


End Sub
 
Upvote 0

SRMPURCHASE

Board Regular
Joined
Dec 23, 2014
Messages
206
Office Version
  1. 2016
Platform
  1. Windows
The last code works just fine, thanks so much.
One more question, can the code be modified to highlight without clearing any other formatting without tying a conditional format into the code?
Although it ignores conditionally formatted cells quite well, I would like to keep some manually set formatting as well.
 
Upvote 0

SRMPURCHASE

Board Regular
Joined
Dec 23, 2014
Messages
206
Office Version
  1. 2016
Platform
  1. Windows
One more question, at Static rng As Range, can I input a range there leaving a number of rows (1 thru 24) out of the VBA and highlight everything below Row 24?
 
Upvote 0

Forum statistics

Threads
1,190,858
Messages
5,983,259
Members
439,833
Latest member
CDaviess

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
Top