We have a dataset of fruits with their delivery information and quantities. We are going to sort those columns independently.
Method 1 – Using VBA Code
Steps:
- Go to the Developer tab.
- Select the Visual Basic command.
- The visual basic window will appear.
- Select Insert.
- Choose Module.
- A module will appear.
- In the module, insert the following code and save it.
Sub Sort_multiple_columns()
Dim xRg As Range 'Declare range for Columns
Dim yRg As Range 'Declare range for Rows
Dim Am_ws As Worksheet 'Name the WorkSheet
Set Am_ws = ActiveSheet
On Error Resume Next
Set xRg = Application.InputBox(Prompt:="Range Selection:", _
Title:="Sort Multiple Columns", Type:=8)
Application.ScreenUpdating = False
For Each yRg In xRg
With Am_ws.Sort
.SortFields.Clear
.SortFields.Add Key:=yRg, Order:=xlAscending
.SetRange Am_ws.Range(yRg, yRg.End(xlDown))
.Header = xlNo
.MatchCase = False
.Apply
End With
Next yRg
Application.ScreenUpdating = True
End Sub
- Go to the Developer tab.
- Choose Macros. You’ll get a Macros window
- Select the code name.
- Click on Run.
- You’ll get an input box.
- Select the B5:F15 range as the desired range.
- Click OK.
- All the columns will be sorted independently.
Method 2 – Sort Multiple Columns in Excel Independently of Each Other by String Length
Steps:
- Select the B13 cell and insert:
=SORTBY(B5:B10, LEN(B5:B10),1)
- Hit the Enter button.
- The B5:B10 range will be sorted by the text length.
- Choose the C13 cell and enter:
=SORTBY(C5:C10, LEN(C5:C10),1)
- Press Enter.
- The C5:C10 range will be sorted according to lower to greater numbers.
- Choose the D13 cell and enter the following formula:
=SORTBY(D5:D10, LEN(D5:D10),1)
- Press the Enter button.
- Consequently, we will have a sorted D5:D10 range according to earlier to later date.
- Select the E13 cell and insert the following formula:
=SORTBY(E5:E10, LEN(E5:E10), 1)
- Press Enter.
- The E5:E10 range will be sorted.
- Click on the F13 cell and type the formula below:
=SORTBY(F5:F10, LEN(F5:F10), 1)
- Hit Enter.
- The column will be sorted by lowest to highest prices.
Formula Explanation
- LEN(F5:F16) will find the length of the selected rows.
- SORTBY function sorts them according to their length number.
Download the Practice Workbook
Further Readings
- How to Sort Alphabetically in Excel with Multiple Columns
- How to Sort Two Columns in Excel to Match
- Sort Columns in Excel Without Mixing Data
<< Go Back to Sort Columns in Excel | Sort in Excel | Learn Excel
Get FREE Advanced Excel Exercises with Solutions!
The first method is wrong. sort by, then by,… does not sort independently
Thanks for the notification, we shall check and update the article.