Sorting Data by Two Columns

surfdog1958

New Member
Joined
May 10, 2015
Messages
2
I'm trying to modify a script I found here (I think) to sort items in a grocery list by Department (Column G), then by Description (Column A). This is the original script, which works fine, but only sorts by Column G:

Sub MASTER_Sort_Active_Rows_BY_DEPARTMENT()


Dim count As Integer

count = WorksheetFunction.CountA(Sheets("MASTER SHEET (Edit & Order)").Range


("A:AS")) - 1

Range("A1:AS1").Select
Range("G1").Activate
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AS" & count).Select
Range("A1").Activate
Selection.Sort Key1:=Range("G1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("G1").Select
End Sub

This is the modified script, which also does what it's supposed to do, but produces an error message:

Sub MASTER_Sort_Active_Rows_BY_DEPARTMENT()


Dim count As Integer

count = WorksheetFunction.CountA(Sheets("MASTER SHEET (Edit & Order)").Range("A:AS")) - 1

Range("A1:AS1").Select
Range("G1").Activate
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AS" & count).Select
Range("A1").Activate
Selection.Sort Key1:=Range("G1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A1").Select
End Sub

What do I need to change?

Thanks in advance. ;)
 

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.

DocAElstein

Banned user
Joined
May 24, 2014
Messages
1,336
Hi surf dog,

. Welcome to the board.
. you explained quite well what you want.
. But try to get clued up on the Board, posting, suggestions, rules etc... get your code in code tags etc...

. As for your request, it would be much easier if we can see what you have and what you want, in addition to any code you have ( In code tags !!!)...


Try and prepare a clear “Before” and “After” “Picture”


. To do that you can either
. 1) - post two screenshots ( see notes for how to do that in my signature –Please do not post am image as we cannot copy that to a spreadsheet!)
. or
. 2) - Up left in the Thread editor is a Table icon. Click that, create an appropriately sized table and fill it in. (To get the table icon up in the Reply window you may need to click on the “Go Advanced” Button next to the Reply Button)

.3 ) – attach two files. ( or better, one File with two sheets, - a “Before” and an “After”.. . For example send over this free thing: Box Net,
Remember to select Share after uploading and give us the link they provide.
………..
. Make sure there is just enough data to make it clear what is needed, so reduce to a maximum of about 40 rows. Remember to desensitize the data – make the data up if you like, as long as it is representative of all possible scenarios and data types.
. - So the “Before” should have just your initial data.
. - The “After”, hand written by you should show exactly how it should look as a result of a Formula or any code based on the exact sample data you give in the BEFORE.


Alan
 

surfdog1958

New Member
Joined
May 10, 2015
Messages
2
I didn't know there were "code tags" and don't see why they're needed when posting subroutines that clearly begin with Sub and end with End Sub. I'd do better to scour the internet for solutions than go to the extremes you're suggesting. How do I unregister?
 

DocAElstein

Banned user
Joined
May 24, 2014
Messages
1,336
I didn't know there were "code tags" and don't see why they're needed when posting subroutines that clearly begin with Sub and end with End Sub. I'd do better to scour the internet for solutions than go to the extremes you're suggesting. How do I unregister?

Hi,
. using code tags is very simple.
. you simply highlight your code and hit the # icon above..

.. so your codes would then come out so:


Code:
Sub MASTER_Sort_Active_Rows_BY_DEPARTMENT()


Dim count As Integer

count = WorksheetFunction.CountA(Sheets("MASTER SHEET (Edit & Order)").Range


("A:AS")) - 1

Range("A1:AS1").Select
Range("G1").Activate
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AS" & count).Select
Range("A1").Activate
Selection.Sort Key1:=Range("G1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("G1").Select
End Sub


Code:
Dim count As Integer

count = WorksheetFunction.CountA(Sheets("MASTER SHEET (Edit & Order)").Range("A:AS")) - 1

Range("A1:AS1").Select
Range("G1").Activate
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AS" & count).Select
Range("A1").Activate
Selection.Sort Key1:=Range("G1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A1").Select
End Sub

.. as with all the Forum Tools, they are not compulsory, but the clearer the "picture" is the more chance you have of someone picking up the Thread.

. Sorry i could not help further.

. I am not sure how you "un register " Possibly try contacting the administrator or log-in and Private Message any moderator.

Alan
 

Watch MrExcel Video

Forum statistics

Threads
1,130,329
Messages
5,641,546
Members
417,219
Latest member
FrancoisT

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
Top