Active Cell

M

Martin

Hello,

I have soem code that doesnt quite work properly and was hoping for some
guidance.

I want to colour the first column of the selected row black, so it
highlights to the user which row they are in. Then when the user moves to
another row the first column of the previous cell reverst back to no colour.
Here is the code so far:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim lRow As Long

For lRow = 34 To 100
If lRow = ActiveCell.Row Then
Cells(Target.Row, 1).Interior.ColorIndex = 1
Else
Cells(Target.Row, 1).Interior.ColorIndex = xlNone
End If

Next

End Sub

I think the problem is with the line:

If lRow = ActiveCell.Row Then

Can anyone help me?

Thanks in advance.

Martin
 
J

JBeaucaire

This is the code I use in my sheets to do what you're talking about, i
always keeps the current row highlighted all the way across the screen
Works great for me

Code
-------------------
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range
Const cnNUMCOLS As Long = 10
Const cnHIGHLIGHTCOLOR As Long = 36 'default lt. yello
Static rOld As Rang
Static nColorIndices(1 To cnNUMCOLS) As Lon
Dim i As Lon
If Not rOld Is Nothing Then 'Restore color indice
With rOld.Cell
If .Row = ActiveCell.Row Then Exit Sub 'same row, don't restor
For i = 1 To cnNUMCOL
.Item(i).Interior.ColorIndex = nColorIndices(i
Next
End Wit
End I
Set rOld = Cells(ActiveCell.Row, 1).Resize(1, cnNUMCOLS
With rOl
For i = 1 To cnNUMCOL
nColorIndices(i) = .Item(i).Interior.ColorInde
Next
.Interior.ColorIndex = cnHIGHLIGHTCOLO
End Wit
End Su
 
S

Stefi

Maybe you want this sub:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range(Cells(34, 1), Cells(100, 1)).Interior.ColorIndex = xlNone
If Target.Row >= 34 And Target.Row <= 100 Then
Cells(Target.Row, 1).Interior.ColorIndex = 1
End If
End Sub

Regards,
Stefi


„Martin†ezt írta:
 
M

Martin

Thank you for sharing that, it works perfectly and is much more advanced than
my code so it will help to increase my understanding.

Many thanks,

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top