highlight active cell Row till "M"

Jagat Pavasia

Active Member
Joined
Mar 9, 2015
Messages
406
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
 
try

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
        Range(Cells(xRow, 1), Cells(xRow, 7)).Interior.ColorIndex = xlNone
        Range(Cells(xRow, 10), Cells(xRow, 12)).Interior.ColorIndex = xlNone
End If
prow = Selection.Row
xRow = prow

With Rng.Interior
    .ColorIndex = 34
    .Pattern = xlSolid
End With
End Sub
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
try

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
        Range(Cells(xRow, 1), Cells(xRow, 7)).Interior.ColorIndex = xlNone
        Range(Cells(xRow, 10), Cells(xRow, 12)).Interior.ColorIndex = xlNone
End If
prow = Selection.Row
xRow = prow

With Rng.Interior
    .ColorIndex = 34
    .Pattern = xlSolid
End With
End Sub

try

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
        Range(Cells(xRow, 1), Cells(xRow, 7)).Interior.ColorIndex = xlNone
        Range(Cells(xRow, 10), Cells(xRow, 12)).Interior.ColorIndex = xlNone
End If
prow = Selection.Row
xRow = prow

With Rng.Interior
    .ColorIndex = 34
    .Pattern = xlSolid
End With
End Sub
YES, THIS CODE IS WORK SOLID AS I WANT.

thank you sir, for whole day helping..

really happy with your help, and you are always happy to help us.
 
Upvote 0
YES, THIS CODE IS WORK SOLID AS I WANT.

thank you sir, for whole day helping..

really happy with your help, and you are always happy to help us.

try

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
        Range(Cells(xRow, 1), Cells(xRow, 7)).Interior.ColorIndex = xlNone
        Range(Cells(xRow, 10), Cells(xRow, 12)).Interior.ColorIndex = xlNone
End If
prow = Selection.Row
xRow = prow

With Rng.Interior
    .ColorIndex = 34
    .Pattern = xlSolid
End With
End Sub
did it possible to work this VBA code below 6th ROW, because from 1st to 5th ROW has title with background color ?
 
Upvote 0
VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Selection.Row >= 6 Then
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
        Range(Cells(xRow, 1), Cells(xRow, 7)).Interior.ColorIndex = xlNone
        Range(Cells(xRow, 10), Cells(xRow, 12)).Interior.ColorIndex = xlNone
End If
prow = Selection.Row
xRow = prow

With Rng.Interior
    .ColorIndex = 34
    .Pattern = xlSolid
End With
Else
If xRow <> "" Then
        Range(Cells(xRow, 1), Cells(xRow, 7)).Interior.ColorIndex = xlNone
        Range(Cells(xRow, 10), Cells(xRow, 12)).Interior.ColorIndex = xlNone
End If
End If
End Sub
 
Upvote 0
Solution
worked 1
VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Selection.Row >= 6 Then
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
        Range(Cells(xRow, 1), Cells(xRow, 7)).Interior.ColorIndex = xlNone
        Range(Cells(xRow, 10), Cells(xRow, 12)).Interior.ColorIndex = xlNone
End If
prow = Selection.Row
xRow = prow

With Rng.Interior
    .ColorIndex = 34
    .Pattern = xlSolid
End With
Else
If xRow <> "" Then
        Range(Cells(xRow, 1), Cells(xRow, 7)).Interior.ColorIndex = xlNone
        Range(Cells(xRow, 10), Cells(xRow, 12)).Interior.ColorIndex = xlNone
End If
End If
End Sub
worked 100% as i want.. thank you very much
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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