Macro to sort by 2 attributes

aprice

New Member
Joined
Jun 16, 2016
Messages
27
I am trying to sort data first by what is in column A and then by what is in column E, in respect to column A. For example, column A has 120 rows listing 6 types of animals with varying times of repetition, and column E has the dates each animal was adopted, with varying times of repetition. I want to group all the animal species together (column A), and then arrange the animals within each group by their adoption date (column E).

I can do one and/or the other, but I can't seem to "nest" them, if that's the right term. I can't use the sort feature because the macro doesn't execute it correctly when I run it on the next day's export.

Thanks!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I can't use the sort feature because the macro doesn't execute it correctly when I run it on the next day's export

Please explain the above statement is more detail with examples of the current day's and next days data (see my signature block below for some ways of posting usable screenshots).
 
Upvote 0
Here is some pretend data from an older question I posted
[TABLE="class: cms_table, width: 226"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Out
[/TD]
[TD]100[/TD]
[TD]6/11[/TD]
[TD]AP[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Out[/TD]
[TD]109[/TD]
[TD]5/21
[/TD]
[TD]MR
[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Out[/TD]
[TD]112[/TD]
[TD]6/03
[/TD]
[TD]JC[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]In[/TD]
[TD]109[/TD]
[TD]6/30
[/TD]
[TD]JC[/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD]In[/TD]
[TD]178[/TD]
[TD]6/11[/TD]
[TD]AP
[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Lost[/TD]
[TD]100[/TD]
[TD]5/24
[/TD]
[TD]AP[/TD]
[/TR]
</tbody>[/TABLE]

It's not a lot to go on, but the info isn't really important to the question. What I would like to do is sort by column A as the priority, and then sort by the dates as secondary. I don't want to sort A, and then rearrange everything to look at it by column C. I have several different reports where I could use this kind of a macro. I just can't figure out how to do priority and secondary sorts.<code>
</code>
 
Upvote 0
It's not a lot to go on, but the info isn't really important to the question

I would think the data is important as you have stated

the macro doesn't execute it correctly when I run it on the next day's export

but you haven't stated why it doesn't execute correctly the next day so if it isn't the data causing the problem the next day what is?

I will look in at this again when I get in tonight from work.
 
Upvote 0
Here is the macro I have to sort by one column, in this case column A. I just need to know what to add so that it will sort by two columns at the same time.

Sub Sort_2()

Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

End Sub
 
Upvote 0
Based on your table posted (but with headers as your posted code uses them) try...

Code:
Sub Sort_2()
Range("A1").CurrentRegion.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("C1"), Order1:=xlAscending, _
 Header:=xlYes, OrderCustom:=1, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End Sub
 
Upvote 0
Based on your table posted (but with headers as your posted code uses them) try...

Code:
Sub Sort_2()
Range("A1").CurrentRegion.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("C1"), Order1:=xlAscending, _
 Header:=xlYes, OrderCustom:=1, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End Sub

Thanks so much! This did exactly what I needed. I just needed to look at how everything was arranged.
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,637
Latest member
Ezio2866

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