Exit sub if I don't change rows

jeffmoseler

Well-known Member
Joined
Jul 16, 2004
Messages
540
I have some code that highlights a row to make it easier to identify what data is selected. It works nice but I had a thought that I could speed up the code if it had a condition where it evaluated whether there was a change in row and exited if there wasn't. Is there a way in VBA to refer to the previously selected cell's row?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
So you want the row which if active to be highlighted is that what you want?
Or does there have to be a change someplace in the row before it is highlighted.

And then next time only the row with a change will be heighted is that what you want.

This script below will always highlight the active cell would that work?

This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Selection.Interior.ColorIndex = 4
Selection.Offset(1).Interior.ColorIndex = 0
If Selection.Row > 1 Then Selection.Offset(-1).Interior.ColorIndex = 0
Selection.Offset(, 1).Interior.ColorIndex = 0
If Selection.Column > 1 Then Selection.Offset(, -1).Interior.ColorIndex = 0
End Sub
 
Last edited:
Upvote 0
I already have code that highlights the active row. What I am looking for is something like this:

IF ActiveCell.Row = PreviousCell.Row Then Exit Sub
 
Upvote 0
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    Application.ScreenUpdating = False
    vk = ActiveSheet.Range("A12", ActiveSheet.Range("A12").End(xlDown)).Rows.Count + 12
    If vk > 30000 Then vk = 13
' Clear the color of all the cells
    Range("A12:AQ" & vk).Interior.ColorIndex = 0
    With Target
' Highlight the entire row and column that contain the active cell
        CurrentRow = ActiveCell.Row
        If CurrentRow >= 12 And CurrentRow <= vk Then
        Range(Cells(CurrentRow, 1), Cells(CurrentRow, 43)).Interior.Color = 6750207
        Else
            Application.EnableEvents = True
            Application.ScreenUpdating = True
            Exit Sub
        End If
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,528
Messages
6,120,064
Members
448,941
Latest member
AlphaRino

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top