vba help - sorting query which is right way sort data

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

In my below code Both gives me correct output , Only there is a change in Key1.

Single cell as key1
Multiple Cells as key1.


Which is Correct approach to sort the data....


VBA Code:
Sub Sorting()

'Complete Range("a2:a10") as sorting key1
Range("a1").CurrentRegion.Sort Key1:=Range("a2:a10"), Order1:=xlAscending, Header:=xlYes

'Single Range("a2")as sorting key1
Range("a1").CurrentRegion.Sort Key1:=Range("a2"), Order1:=xlAscending, Header:=xlYes
    
End Sub

Below is Sample data

Book4
ABC
1Datedata1data2
201/11/2020xxxyyyy
309/11/2020xxxyyyy
405/11/2020xxxyyyy
506/11/2020xxxyyyy
609/11/2020xxxyyyy
703/11/2020xxxyyyy
Sheet1


Thanks
mg
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You can sort the data either way, by selecting the region or selecting the header. But you limit yourself if you select the range, since any new data will not be sorted. So the better soln is to just select the header, and excel will look at all rows under that header automatically.
 
Upvote 0
Hi ManiacB,

Thanks for your help, I got it


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,603
Members
449,089
Latest member
Motoracer88

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