Solved

Highlight the active row

Posted on 2012-04-06
12
717 Views
Last Modified: 2012-04-07
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!
0
Comment
Question by:calbais
  • 7
  • 5
12 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37816408
Remove line 74.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37816480
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
 

Author Comment

by:calbais
ID: 37817858
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
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37817863
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
HighLight Target.Row, Target.Column
End Sub
0
 

Author Comment

by:calbais
ID: 37817905
I can only enter data in Column A.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37817916
I have to leave for a few hours. I'll fix this when I get back.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 45

Expert Comment

by:Martin Liss
ID: 37817951
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
 

Author Comment

by:calbais
ID: 37818002
No, same thing. I can only change columns by using the mouse and that doesn't highlight the row.
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 37819021
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
 

Author Comment

by:calbais
ID: 37819643
Yes. That works in my application!! Thank you very much for your skill and time!!
0
 

Author Closing Comment

by:calbais
ID: 37819645
Solution works just fine!!
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 37819877
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now