Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Highlight the active row

Posted on 2012-04-06
12
Medium Priority
?
737 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 49

Expert Comment

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

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
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 49

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 49

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 49

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 49

Accepted Solution

by:
Martin Liss earned 2000 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 49

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Gain an elementary understanding of Blockchain technology.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

688 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