VBA To Sort Data Based On Column Values

excelnube

Board Regular
Joined
Jul 14, 2011
Messages
65
Hi guys,

I have a simple problem, or at least i believe its simple.

I have a spreadsheet that helps me to tracks orders. I want to be able to add a bit code to my current VBA script that sorts the data based on the values in a particular column (smallest to largest).

Current table looks like this:

[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD]Order Number
[/TD]
[TD]Date
[/TD]
[TD]Quantity
[/TD]
[TD]Colour
[/TD]
[TD]Priority
[/TD]
[/TR]
[TR]
[TD]548562
[/TD]
[TD]12-09-2012
[/TD]
[TD]56
[/TD]
[TD]VBA formula
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]325415
[/TD]
[TD]01-04-2012
[/TD]
[TD]102
[/TD]
[TD]VBA formula
[/TD]
[TD]1
[/TD]
[/TR]
[TR]
[TD]012103
[/TD]
[TD]06-10-2012
[/TD]
[TD]12
[/TD]
[TD]VBA formula
[/TD]
[TD]2
[/TD]
[/TR]
[TR]
[TD]025486
[/TD]
[TD]26-08-2012
[/TD]
[TD]2
[/TD]
[TD]VBA formula
[/TD]
[TD]3
[/TD]
[/TR]
[TR]
[TD]520148
[/TD]
[TD]01-01-2012
[/TD]
[TD]54
[/TD]
[TD]VBA formula
[/TD]
[TD]4
[/TD]
[/TR]
</tbody>[/TABLE]

I want to add be able to sort the data, smallest to largest based on Column E (tittle Priority)

Any help would be appreciated.

Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
excelnube,


Sample raw data (the Yellow cells contain a formula, but you did not give us the formula):


Excel Workbook
ABCDE
1Order NumberDateQuantityColourPriority
254856212/9/20125622
33254151/4/201210211
4121036/10/20121222
52548626-08-2012233
65201481/1/20125444
7
Sheet1





After the macro:


Excel Workbook
ABCDE
1Order NumberDateQuantityColourPriority
23254151/4/201210211
354856212/9/20125622
4121036/10/20121222
52548626-08-2012233
65201481/1/20125444
7
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub SortMyData()
' hiker95, 10/08/2012
' http://www.mrexcel.com/forum/excel-questions/663272-visual-basic-applications-sort-data-based-column-values.html
Dim lr As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("A2:E" & lr).Sort key1:=Range("E2"), order1:=1
Application.ScreenUpdating = True
End Sub


Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm


Then run the SortMyData macro.


If the screenshot of after the macro is not correct, then please supply another screenshot of the results you are looking for.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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
Back
Top