highlight active cell Row till "M"

Jagat Pavasia

Active Member
Joined
Mar 9, 2015
Messages
359
Office Version
  1. 2021
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
 
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: 5
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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
Upvote 0
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?
 
Upvote 0
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 ??
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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...
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

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