highlight active cell Row till "M"

Jagat Pavasia

Board Regular
Joined
Mar 9, 2015
Messages
244
Office Version
  1. 2019
Platform
  1. Windows
I have VBA code to highlight active Row, it is working very well.
but it is highlight whole Row.
I want hightlight in my data entry range.

my data is typed from Row "A" to "M".

please edit VBA code, I am new user in VBA

code Below :

VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

Static xRow

If xRow <> "" Then
    With Rows(xRow).Interior
        .ColorIndex = xlNone
    End With
End If
pRow = Selection.Row
xRow = pRow
With Rows(pRow).Interior
    .ColorIndex = 34
    .Pattern = xlSolid
End With
End Sub
 

Jagat Pavasia

Board Regular
Joined
Mar 9, 2015
Messages
244
Office Version
  1. 2019
Platform
  1. Windows
I m not sure I get this....
What do you mean by "it is got blank after selection""???
look at this IMAGE below,
my column H, J, M ,N,O have fill color. it is remove after selection.
In Image, the Row 11 to row 14 is got blank color after selection.
 

Attachments

  • Capture.JPG
    Capture.JPG
    108.5 KB · Views: 3

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Jagat Pavasia

Board Regular
Joined
Mar 9, 2015
Messages
244
Office Version
  1. 2019
Platform
  1. Windows
look at this IMAGE below,
my column H, J, M ,N,O have fill color. it is remove after selection.
In Image, the Row 11 to row 14 is got blank color after selection.

look at this IMAGE below,
my column H, J, M ,N,O have fill color. it is remove after selection.
In Image, the Row 11 to row 14 is got blank color after selection.
look at this, I have fill RED color in new worksheet, but when I select row then automatically removed red color.


please help me in this case.
 

Attachments

  • Capture.PNG
    Capture.PNG
    16.3 KB · Views: 1

fadee2

Active Member
Joined
Nov 7, 2020
Messages
367
Office Version
  1. 2019
Platform
  1. Windows
This is the default behavior.
Since you are asking excel to fill the row color upon cell selection. Ultimately your previous background color will automatically be removed.

if I may ask, what is the purpose of this color fill you are doing via vba?
 

Jagat Pavasia

Board Regular
Joined
Mar 9, 2015
Messages
244
Office Version
  1. 2019
Platform
  1. Windows
This is the default behavior.
Since you are asking excel to fill the row color upon cell selection. Ultimately your previous background color will automatically be removed.

if I may ask, what is the purpose of this color fill you are doing via vba?
I have color fill in some whole column in my sheet for easy viewing And I want to highlight the sell which i select the row.

But after this VBA my cell with colour fill is going default blank colour,

Any other way to remain color as it is in my sheet ??
 

fadee2

Active Member
Joined
Nov 7, 2020
Messages
367
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

try
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Range(Cells(Selection.Row, 1), Cells(Selection.Row, 13)).Select
End Sub

and then double click on any cell in the row you want to select.
 

Jagat Pavasia

Board Regular
Joined
Mar 9, 2015
Messages
244
Office Version
  1. 2019
Platform
  1. Windows
try
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Range(Cells(Selection.Row, 1), Cells(Selection.Row, 13)).Select
End Sub

and then double click on any cell in the row you want to select.
it is not useful for me.
 

Jagat Pavasia

Board Regular
Joined
Mar 9, 2015
Messages
244
Office Version
  1. 2019
Platform
  1. Windows
do u want highlight complete row or only specific cell in that row??
Yes, I want highlight selection from A to G & J to L. (Which has no background colour)

Did it possible ??
 

fadee2

Active Member
Joined
Nov 7, 2020
Messages
367
Office Version
  1. 2019
Platform
  1. Windows
If you only want to select the rows using excel's default selection method, then (on doubleclick)

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Set rng1 = Range(Cells(Selection.Row, 1), Cells(Selection.Row, 7))
Set rng2 = Range(Cells(Selection.Row, 10), Cells(Selection.Row, 12))
Set Rng = Union(rng1, rng2)
Rng.Select
End Sub

If you want to use your own code to highlight the row, then (on singleclick)

VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

Set rng1 = Range(Cells(Selection.Row, 1), Cells(Selection.Row, 7))
Set rng2 = Range(Cells(Selection.Row, 10), Cells(Selection.Row, 12))
Set Rng = Union(rng1, rng2)
Static xRow

If xRow <> "" Then
    With Rows(xRow).Interior
        .ColorIndex = xlNone
    End With
End If
prow = Selection.Row
xRow = prow
With Rng.Interior
    .ColorIndex = 34
    .Pattern = xlSolid
End With
End Sub

HTH...
 

Jagat Pavasia

Board Regular
Joined
Mar 9, 2015
Messages
244
Office Version
  1. 2019
Platform
  1. Windows
If you only want to select the rows using excel's default selection method, then (on doubleclick)

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Set rng1 = Range(Cells(Selection.Row, 1), Cells(Selection.Row, 7))
Set rng2 = Range(Cells(Selection.Row, 10), Cells(Selection.Row, 12))
Set Rng = Union(rng1, rng2)
Rng.Select
End Sub

If you want to use your own code to highlight the row, then (on singleclick)

VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

Set rng1 = Range(Cells(Selection.Row, 1), Cells(Selection.Row, 7))
Set rng2 = Range(Cells(Selection.Row, 10), Cells(Selection.Row, 12))
Set Rng = Union(rng1, rng2)
Static xRow

If xRow <> "" Then
    With Rows(xRow).Interior
        .ColorIndex = xlNone
    End With
End If
prow = Selection.Row
xRow = prow
With Rng.Interior[CODE=vba]
.ColorIndex = 34
.Pattern = xlSolid
End With
End Sub[/CODE]

HTH...

this is not working,
same the background color remove from whole ROW.





VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

Set rng1 = Range(Cells(Selection.Row, 1), Cells(Selection.Row, 7))
Set rng2 = Range(Cells(Selection.Row, 10), Cells(Selection.Row, 12))
Set Rng = Union(rng1, rng2)
Static xRow

If xRow <> "" Then
    With Rows(xRow).Interior
        .ColorIndex = xlNone
    End With
End If
prow = Selection.Row
xRow = prow
With Rng.Interior
    .ColorIndex = 34
    .Pattern = xlSolid
End With
End Sub
 

Forum statistics

Threads
1,144,394
Messages
5,724,087
Members
422,536
Latest member
Zeeshan53

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