Solved

Highlight the active row

Posted on 2012-04-06
12
722 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
 
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

With the internet and the ease of information transference, many professional jobs can be done anywhere today.  Why should it make a difference whether an x-ray is read in India or the United States as long as the radiologist is qualified?   Outso…
Meetings to discuss business process can waste time, and often do .  The meeting's dialog can get confusing when participants have different professional perspectives and backgrounds.  A jointly-developed process picture helps wade through the confu…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

937 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

9 Experts available now in Live!

Get 1:1 Help Now