Sort in a worksheet change event

Xenowyn

New Member
Joined
Jul 22, 2014
Messages
30
So I have the following code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
If Target.Column = 1 Then
    Columns(1).Sort key1:=Range("A3"), order1:=xlAscending, Header:=xlYes
End If
Application.EnableEvents = True

End Sub

I want to sort the values in column A in ascending order (the header consists of rows 1 and 2 merged) only if a cell in column A is changed. But when I change a cell in column A with this code in place, nothing happens. Any help?
 
There is a work-around to keep the changed cell Active after the sort. But it depends on the values in column A being unique.
Are your col A values unique?
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
In that case, try this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim tValue As Variant, rOffset As Long, cOffset As Long
    
    With Target
        If .Column = 1 Then
            tValue = .Cells(1, 1).Value
            rOffset = ActiveCell.Row - .Row
            cOffset = ActiveCell.Column - .Column
            
            With Columns(1)
                With Range(.Cells(3, 1), .Cells(Rows.Count, 1).End(xlUp)).EntireRow
                    .Sort key1:=.Cells(1, 1), order1:=xlAscending, Header:=xlNo
                End With
                With .Find(tValue, after:=.Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
                    Application.Goto .Cells
                    If .Row = 3 And rOffset = -1 Then rOffset = 0
                    .Offset(rOffset, cOffset).Select
                End With
            End With
        End If
    End With
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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