Sort Macro Help

Toshiba / Satellite l500
November 11, 2010 at 11:54:23
Specs: Windows Vista, 2 GHz / 2936 MB
I have the following macro used to sort a protected excel sheet. I can get it to sort LastName (Column B). My issue is i can't get it to sort Last Name (Column B) First Name (Column A). I can only make it sort via one or the other. I can't get it to sort last name then first name, so if i have multiple same last names, those ones would be sorted alphabetically by there first name's. This is in excel 2007

Sub SortLastName()
ActiveSheet.Unprotect Password:="password here"
Range("A3:K1000").Sort Key1:=Range("B1"), _
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, _
ActiveSheet.Protect Password:="password here"
End Sub

See More: Sort Macro Help

Report •

November 11, 2010 at 12:22:24
I can't test 2007 at work (only 2003) but it shouldn't make any difference in this matter.

Record a macro while sorting first on Column B, then on Column A - in one operation.

The resulting code can then be placed between your Unprotect/Protect lines. You should be able to see the difference between what you have and what is needed to do a "2 column sort".

BTW...I am assuming that you are also protecting the VBA code so that the user can't see the password and unprotect the sheet manually.

Posting Tip: Before posting Data or VBA Code, read this How-To.

Report •

November 11, 2010 at 23:09:44

Thanks for the suggestion about using the record macro function. I didnt even notice it. Once i did that it made coding the macro a great deal easier. Yes i also protect the code for the VBA code as well.

Report •

November 12, 2010 at 04:55:35
Just keep in mind that the recorder adds a lot of extra code to your macros that you should clean up afterwards. If you don't, you end up with bloated, ineffcient code that is hard to follow.

You may not have seen that with the simple Sort that you recorded, but trust me, it can get ugly sometimes.

Last week someone posted a recorded macro and I pointed out that close to 150 lines of code could be reduced to 6. Every Select, Scroll, Activate etc. had been recorded, and none of it was needed.

Posting Tip: Before posting Data or VBA Code, read this How-To.

Report •

Related Solutions

Ask Question