Highlight the active row

Could someone modify the following code which I found online? It highlights the row as I want but every time I press the up or down arrow, the active cell moves one space to the right. I want it to stay in the same column, just move up or down.

Also, as it is, it only works if you use the keyboard to move around the worksheet. I would like it to highlight the row even if a cell is clicked with the mouse.

I tried Conditional Formatting to do this and it worked fine by itself but I also use CF to highlight duplicate entries in my worksheet and they won't work together. I'd prefer to leave the CF find duplicates and use this code to highlight the rows.

Here's the code:

Option Explicit
'// Placed in the ThisWorkbook Object

Private Sub Workbook_Open()
    Application.OnKey "{RIGHT}", "HighlightRight"
    Application.OnKey "{LEFT}", "HighlightLeft"
    Application.OnKey "{UP}", "HighlightUp"
    Application.OnKey "{DOWN}", "HighlightDown"
    Application.OnKey "{DEL}", "DisableDelete"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnKey "{RIGHT}"
    Application.OnKey "{LEFT}"
    Application.OnKey "{UP}"
    Application.OnKey "{DOWN}"
    Application.OnKey "{DEL}"
End Sub

-----------------------------------------------------------------------------------------------------------------------
Option Explicit
'/////////////////////////////////
'// Original by NateO for aldo ///
'//     24th Jan 2003          ///
'//    Amended by IFM          ///
'//     28th Jan 2003          ///
'//    Amended by Aldo         ///
'//
'/////////////////////////////////

'// Placed in a Std Module

'Dim strCol As String
'Dim iCol As Integer
Dim dblRow As Double

Sub HighlightRight()
    HighLight 0, 1
End Sub

Sub HighlightLeft()
    HighLight 0, -1
End Sub

Sub HighlightUp()
    HighLight -1, 0, -1
End Sub

Sub HighlightDown()
    HighLight 1, 0, 1
End Sub

Sub HighLight(dblxRow As Double, iyCol As Integer, Optional dblZ As Double = 0)

'// Amended to highlight Activecell cross intersection
'// Amended as an Alternative to using Condtional Formats
'// As per Aldo thread;
'// http://216.92.17.166/board/viewtopic.php?topic=19239&forum=2&start=20&22

On Error GoTo NoGo
'strCol = Mid(ActiveCell.Offset(dblxRow, iyCol).Address, _
        InStr(ActiveCell.Offset(dblxRow, iyCol).Address, "$") + 1, _
        InStr(2, ActiveCell.Offset(dblxRow, iyCol).Address, "$") - 2)
'iCol = ActiveCell.Column
dblRow = ActiveCell.Row

'// If you don't want screen flicker
Application.ScreenUpdating = False
    
With Range(dblRow + dblZ & ":" & dblRow + dblZ)
    .Select
    '// Need to reset here!
    Application.ScreenUpdating = True
    .Item(dblRow + dblxRow).Activate
End With

NoGo:
End Sub


Sub DisableDelete()
    Cells(ActiveCell.Row, ActiveCell.Column).Select
    Application.OnKey "{DEL}"
End Sub

Sub ReSet()
    Application.OnKey "{RIGHT}"
    Application.OnKey "{LEFT}"
    Application.OnKey "{UP}"
    Application.OnKey "{DOWN}"
End Sub

Open in new window


Thanks for any help with this!
calbaisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Remove line 74.
0
Martin LissOlder than dirtCommented:
This is probably better: (there are 3 changes are marked with 'new)


Option Explicit
'/////////////////////////////////
'// Original by NateO for aldo ///
'//     24th Jan 2003          ///
'//    Amended by IFM          ///
'//     28th Jan 2003          ///
'//    Amended by Aldo         ///
'//
'/////////////////////////////////

'// Placed in a Std Module

'Dim strCol As String
'new
Dim iCol As Integer

Dim dblRow As Double

Sub HighlightRight()
    HighLight 0, 1
End Sub

Sub HighlightLeft()
    HighLight 0, -1
End Sub

Sub HighlightUp()
    HighLight -1, 0, -1
End Sub

Sub HighlightDown()
    HighLight 1, 0, 1
End Sub

Sub HighLight(dblxRow As Double, iyCol As Integer, Optional dblZ As Double = 0)

'// Amended to highlight Activecell cross intersection
'// Amended as an Alternative to using Condtional Formats
'// As per Aldo thread;
'// http://216.92.17.166/board/viewtopic.php?topic=19239&forum=2&start=20&22

On Error GoTo NoGo
'strCol = Mid(ActiveCell.Offset(dblxRow, iyCol).Address, _
        InStr(ActiveCell.Offset(dblxRow, iyCol).Address, "$") + 1, _
        InStr(2, ActiveCell.Offset(dblxRow, iyCol).Address, "$") - 2)
'new
iCol = ActiveCell.Column

dblRow = ActiveCell.Row

'// If you don't want screen flicker
Application.ScreenUpdating = False
    
With Range(dblRow + dblZ & ":" & dblRow + dblZ)
    .Select
    'new
    .Item(iCol).Activate
    
    '// Need to reset here!
    Application.ScreenUpdating = True
    '.Item(dblRow + dblxRow).Activate
End With

NoGo:
End Sub


Sub DisableDelete()
    Cells(ActiveCell.Row, ActiveCell.Column).Select
    Application.OnKey "{DEL}"
End Sub

Sub ReSet()
    Application.OnKey "{RIGHT}"
    Application.OnKey "{LEFT}"
    Application.OnKey "{UP}"
    Application.OnKey "{DOWN}"
End Sub

Open in new window

0
calbaisAuthor Commented:
Thanks Martin. That makes it stay in one column but it won't move to any other column unless I click the mouse and when I do that, the row is not highlighted.

So, if it could be changed so that I can use the keyboard or the mouse to move to any cell and that row will be highlighted.

Thanks
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Martin LissOlder than dirtCommented:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
HighLight Target.Row, Target.Column
End Sub
0
calbaisAuthor Commented:
I can only enter data in Column A.
0
Martin LissOlder than dirtCommented:
I have to leave for a few hours. I'll fix this when I get back.
0
Martin LissOlder than dirtCommented:
I actually had a couple of minutes (my wife is calling me now!!!). If that doesn't fix it I will fix it when I get back.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'HighLight Target.Row, Target.Column
Range(Target.Row & ":" & Target.Row).Select
Target.Activate
End Sub
0
calbaisAuthor Commented:
No, same thing. I can only change columns by using the mouse and that doesn't highlight the row.
0
Martin LissOlder than dirtCommented:
I'm sorry for all the failed attempts. I made a couple of small changes. I also attached a working demo project just in case.

Sub HighLight(dblxRow As Double, iyCol As Integer, Optional dblZ As Double = 0)

'// Amended to highlight Activecell cross intersection
'// Amended as an Alternative to using Condtional Formats
'// As per Aldo thread;
'// http://216.92.17.166/board/viewtopic.php?topic=19239&forum=2&start=20&22

On Error GoTo NoGo
iCol = ActiveCell.Column

dblRow = ActiveCell.Row

'// If you don't want screen flicker
Application.ScreenUpdating = False
    
With Range(dblRow + dblZ & ":" & dblRow + dblZ)
    .Select
    'new
    .Item(iCol + iyCol).Activate
    
    '// Need to reset here!
    Application.ScreenUpdating = True

End With

NoGo:
End Sub

Open in new window


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'new
Application.ScreenUpdating = False

Range(Target.Row & ":" & Target.Row).Select
Target.Activate
'new
Application.ScreenUpdating = True

End Sub

Open in new window

Book1.xls
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
calbaisAuthor Commented:
Yes. That works in my application!! Thank you very much for your skill and time!!
0
calbaisAuthor Commented:
Solution works just fine!!
0
Martin LissOlder than dirtCommented:
I'm glad I was able to help.

My profile contains links to some articles that I've written. The one on debugging may be of interest to you.
Marty - MVP 2009 to 2012
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office Productivity

From novice to tech pro — start learning today.