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:

Order Number
Date
Quantity
Colour
Priority
548562
12-09-2012
56
VBA formula
2
325415
01-04-2012
102
VBA formula
1
012103
06-10-2012
12
VBA formula
2
025486
26-08-2012
2
VBA formula
3
520148
01-01-2012
54
VBA formula
4

<tbody>
</tbody>

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 total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
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,215,606
Messages
6,125,811
Members
449,262
Latest member
hideto94

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