Solved

Highlight the active row

Posted on 2012-04-06
12
723 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 46

Expert Comment

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

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 46

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 46

Expert Comment

by:Martin Liss
ID: 37817916
I have to leave for a few hours. I'll fix this when I get back.
0
 
LVL 46

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 46

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 46

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

770 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