|
Sort Data With Headers in Descending Order in Excel
This Excel macro sorts data that has headers in descending order. This means that data is sorted Z to A and 10 to 1 - or reverse alphabetical order and highest to lowest. This macro also assumes that there is one row of headers at the top of your data set or data range. If you have more than one row of headers on your data, simply make the range reference to the entire table start one row below the top header row - this will take into account two rows for headers.
This is a great and simple sorting macro in Excel and will help to get you started or will fit nicely into existing macros that you have.
To use this macro, simply replace A1:C56 with the range reference of your entire data set or data table that you want to be sorted and then replace A1 with the cell reference of the row that you would like to sort the rest of the data by in descending order.
Where to install the macro: Module
Excel Macro to Sort Data With Headers in Descending Order
Sub Sort_Descending_Basic_With_Header()
'Sorts a worksheet in descending order and assumes there are headers on the data
Range("A1:C56").Sort _
Key1:=Range("A1"), Order1:=xlDescending, Header:=xlYes
End Sub
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007 Go to Tools > Macros > Visual Basic Editor
For Excel 2007 Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.
Similar Helpful Excel Resources
Is it possible to sort two different ranges of data on seperate tabs in descending order using a macro?
I would like sort data in both tabs by order of volume while keeping all data in tact. Also I do not want to include the header or "total" row in the sort.
Please refer to the .xls file for reference.
Thanks!
Trying to figure out how to sort in Descending order.
Code:
rst.Open "SELECT DISTINCT [" & str & "] FROM qry_Sol" & ComboBox3.Value & " ORDER BY [" & str & "] ;", cnn, adOpenStatic
Please see attached doc.
The four macro buttons change the graph to show the specific month. Is there a way where i can sort the bars out so that the longest bar is at the top and the shortest at the bottom, or visa versa.
Like data sorting a table, but sorting the graph instead.
Thanks for your help...
Hi,
Is it possible to sort a range of data in ascending and descending order using just one macro? I can have two macros with the alternate choices on but I would really like just the one option.
I can do it if there is numerical data using this code:
Code:
Sub Sortcreation()
Dim xlSort As XlSortOrder
With Range("A4:IV23")
If Range("A4") > Range("A5") Then
xlSort = xlAscending
Else
xlSort = xlDescending
End If
.Sort Key1:=Range("A4"), Order1:=xlSort, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With
End Sub
Any ideas?
Thank you in advance.
JP
Hi
I have a field which contains dates which are in "mmm-yy" format. The most recent dates appear at the bottom of the list.
how can i sort the list items within the autofilter drop down to appear in descending order?
Thanks
Vincent
Hi Guys,
I have a sheet, looking like that
product1
product2
product3
Total1
product1
product2
product3
Total2
product1
product2
Total3
What I want to achieve is if the total is positive, then sort the range above it in descending order, if the total is negative then sort t in ascending order, I would like that as a VBA since I need to execute after some other existing code
As in my simple example above I will have multiple range to sort, the cell with total are fixed so they can be 'hardcoded' in the vba, also the amount for the products can be positive or negative.
Thanks a lot!
Hi
I have a field which contains dates which are in "mmm-yy" format. The most recent dates appear at the bottom of the list.
how can i sort the list items within the autofilter drop down to appear in descending order?
If you can help then please post answers on the thread below:
http://excelforum.com/showthread.php...23#post1955323
Thanks
Vincent
Hi,
This one always seems to get me.
I have a pivot table setup like this -
I have 5 fields in my rows, and one field in my data section.
It is a simple employee pivot table -
The 5 rows are -
Department, Employee Name, Start Date, Term Date, Level and the data field is Salary.
I would like to sort the pivot table, so it shows -
The Department in Ascending order, than the Employee Names in Ascending Order, and then the salary (Data Field) in Descending order.
For some reason I can get it done.
Any help would be much appreciated.
Thanks,
Hi all,
I am new to using macros and need some help. Currently using Excel 2007.
I have an Excel file that has many rows of data in columns A to G. Row 1 is the header row.
I need to be able to select x number of rows and put them in a separate worksheet in the same workbook with the header row.
Is it also possible to sort the new worksheet by Column C from Largest to Smallest?
I found the following script in another post:
http://excel.bigresource.com/Track/excel-gi1t3xnG/
However, it doesn't seem to save to another worksheet in the file.
Thanks,
Stan
|
|