0

I have a code that sorts the entries i put from a userform.

In column C, I have the project name and in column D i have the position. The code currently sorts based off of Column C in the worksheet.

The issue I am having is that i need it to Sort based off of column C first and then Column B. Currently the code Finds entries matching in column C and just adds to the bottom row.

Sub Sortit() With Sheet1 .Range("B5:W10000").Sort Key1:=.Range("C3"), Header:=xlNo End With End Sub 
3
  • What's in the first 4 rows, and why isn't the key one of the headings? Wait, there's no header?? Can a header be added? Commented Oct 16, 2017 at 19:29
  • The Column Headers are in C4-K4, First 3 rows are empty Commented Oct 16, 2017 at 19:32
  • It's not clear why your range starts at B5 then. Have you tried .Range("B4:K10000").Sort Key1:=.Range("C4"), Header:=xlYes? Also I'd warmly recommend removing the empty lines and converting your range to a table/ListObject. Commented Oct 16, 2017 at 19:42

2 Answers 2

1

This sub works for me to sort a worksheet by two columns- first by A and then by B. I believe that is answering your question although it is a little unclear.

Sub sort() Range("A1:C7").Select Selection.sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("B1") _ , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _ False, Orientation:=xlTopToBottom End Sub 

Example of final state-

A B C blj asd b blj er d blj fd c dflkj 6 e ldkfj 7 a ldkfj 7 f 
Sign up to request clarification or add additional context in comments.

1 Comment

Philip the code works great! the only issue is that after the entry from the userform is in the worksheet and sorted, the whole worksheet is selected. An easy fix
0

I would mark this question as a duplicate but I don't have the needed rep points. You should find your answer in VBA Excel sorting on multiple columns should be something like...

.Sort_ key1:=col1,order1:=xlAscending,DataOption1:=xlSortNormal, _ key2:=col2, order2:=xlAscending, DataOption2:=xlSortNormal, _ key3:=col3, order3:=xlAscending, DataOption3:=xlSortNormal, _ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.