Auto Sorting Macro

dtrusnik06

New Member
Joined
Feb 2, 2018
Messages
1
I am using the following macro to auto sort entire rows by dates entered into column "N". The first row in the data that needs to be sorted is "4". I will need this to auto sort any rows that are added into worksheet and dates entered. It was working fine but it just started not auto sorting entire row, it only auto sorts the dates in column "N". Is there any fix that will solve this for me?

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("N:N")) Is Nothing Then
Range("N4").Sort Key1:=Range("N5"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub
 

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.
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("N:N")) Is Nothing Then
Range("A4:N[COLOR=#ff0000]20000[/COLOR]").Sort Key1:=Range("N5"), _
Order1:=xlAscending, header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub
Where the number in red is large enough to cover all your data
 
Upvote 0
That can happen if you have any blank columns in your row.

We can find dynamically find the last row in column N with data, and tell it to sort the entire rows, i.e.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lr As Long

    On Error Resume Next
    
    If Not Intersect(Target, Range("N:N")) Is Nothing Then
'   Find last row in column N
    lr = Cells(Rows.Count, "N").End(xlUp).Row
'   Sort entire rows, starting on row 4
    Rows("4:" & lr).Sort Key1:=Range("N5"), _
        Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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