Avatar of ca1358
ca1358
 asked on

Categorize and Sort Excel VBA

Is there any way to have  “mouse” categorize at the top and date sorted ,
then “Dog”, then ”cat”
and when a new line is add, when Column B is entered, this would categorize it again and date ordered?


Dog      10/25/2010
cat      8/19/2010
mouse      8/16/2010
Dog      11/1/2010
mouse      12/1/2010
Dog      8/16/2010
cat      7/1/2010
mouse      11/8/2010
Dog      11/8/2010
cat      10/1/2010
Visual Basic ClassicMicrosoft Excel

Avatar of undefined
Last Comment
Tracy

8/22/2022 - Mon
TommySzalapski

Does mouse have to be at the top? You could just sort by column B then by column A.
ca1358

ASKER
Yes order would have to be Mouse, Dog, Cat
TommySzalapski

Just sort it descending then.
Add this code to the worksheet to make it automatic.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
  Range("A:B").SortSpecial Key1:=Range("A1"), Order1:=xlDescending, Key2:=Range("B1"), Order2:=xlAscending
End If
End Sub

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
gbanik

Have a helper column that stores 1,2 or 3 based on the column containing Mouse, Dog or Cat. Now sort as you want... based on the helper column and dates.
ASKER CERTIFIED SOLUTION
TommySzalapski

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
TommySzalapski

gbanik, that was also for your benefit. Custom lists are really cool. They work for sorting and fill down.
ca1358

ASKER
I just realize it would be descending but it would not always be the case.  They could be mix up, so how do I define the list once sorted?   So that mouse, cat, dog.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
TommySzalapski

You just need to define a custom list. Which version of Excel are you using?
TommySzalapski

2007/2010 from the link I posted:
1.Click the Microsoft Office Button , and then click Excel Options.
2.Click the Popular category, and then under Top options for working with Excel, click Edit Custom Lists.
3.In the Custom Lists box, click NEW LIST, and then type the entries in the List entries box, beginning with the first entry.
Press ENTER after each entry.

4.When the list is complete, click Add.
The items in the list that you selected are added to the Custom lists box.

5.Click OK twice.
TommySzalapski

http:#a34086942 Is the best answer. Custom lists work really well for sorting.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Tracy

This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.