Some Doubts about Sorting

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
684
Hello

I want to clear some doubts about sorting

the following code sorted perfectly

Just
1. When rows of records are entered after sorting. Then want to sort it with new records it does not sort. I dont understand Why ?
2. So when do i need to sort. Any particular criteria required for sorting
3. Also my P2 cell has header Value though in the below code Header:=xlNo and data starts from row 3 so do i need to change to
Sort Key1:=Range("P3")

Code:
Private Sub cmdSorting_Click()

Dim ws As Worksheet
Set ws = Worksheets("MIS")
ws.Activate

Dim lastRow As Long

lastRow = ws.Cells(ws.Rows.Count, 2).End(xlUp).row
ws.Range("A3:BC" & lastRow).Sort Key1:=Range("P2"), Order1:=xlAscending, Header:=xlNo

End Sub
NimishK
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Key1 should be in the range being sorted and should reference to the same sheet as the range

amendment to original code
Code:
ws.Range("A3:BC" & lastRow).Sort Key1:=[COLOR=#ff0000]ws[/COLOR].Range("[COLOR=#ff0000]P3[/COLOR]"), Order1:=xlAscending, Header:=xlNo

alternative for you

Code:
Private Sub cmdSorting_Click()
    With Worksheets("MIS")
        .Activate
        .Range("A3:BC" & Rows.Count).Sort Key1:=[COLOR=#ff0000].[/COLOR]Range("P3"), Order1:=xlAscending, Header:=xlNo
    End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,517
Messages
6,114,085
Members
448,548
Latest member
harryls

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