|
Sort Worksheet Tabs - Ascending or Descending Order
This macro will sort all of the worksheets in the current workbook. It can sort in ascending or descending order. This will work to sort by number and or also alphabetically. This type of sort is conducted through a bubble sort type.
This is ideal if you work with a large number of tabs or wroksheets in excel documents and you need to put them in order; this is especially helpful if you have to import or receive often messy workbooks or excel files.
Where to install the macro: Module
Sort Worksheet Tabs - Ascending or Descending Order
Sub Sort_Worksheets()
Dim i As Integer
Dim j As Integer
Dim iAnswer As VbMsgBoxResult
iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
& "Clicking No will sort in Descending Order", _
vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
For i = 1 To Sheets.Count
For j = 1 To Sheets.Count - 1
If iAnswer = vbYes Then
If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
Sheets(j).Move After:=Sheets(j + 1)
End If
ElseIf iAnswer = vbNo Then
If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then
Sheets(j).Move After:=Sheets(j + 1)
End If
End If
Next j
Next i
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
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 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,
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 everyone.
I need two similar macros: one each for sorting worksheets in ascending/descending order based on the value of the same cell in each worksheet.
The first and last worksheets (named as "cover page" and "summary") need to remain where they are, but every worksheet in between these needs to be sorted based on the value of cell A5 in each.
Any help will be highly appreciated.
Thanks in advance.
Danny
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
Everywhere I've read regarding sorting Arrays in Ascending / Descending order with VBA says there is no built in sort function that will do this...
Can someone help me out with with a sorting algorithm that will accomplish this?
Thanks.
I found this Macro and it works for what I need. Except that the results end up in Ascending Order and I would prefer it to end up in Descending Order.
Any Help?
Code:
Private Sub CommandButton1_Click()
Dim I As Integer
Dim J As Integer
Dim K As Integer
Dim L As Integer
For I = 1 To 61 '61 being the last row with data in it
J = Worksheets("Sheet1").Range("C" & Trim(Str(I))).Value
K = Worksheets("Sheet1").Range("D" & Trim(Str(I))).Value
For L = J To K 'now we're going to write the values out to Sheet 2
Worksheets("Sheet2").Range("A" & Trim(Str(L))).Value = Worksheets("Sheet1").Range("A" & Trim(Str(I))).Value
Worksheets("Sheet2").Range("B" & Trim(Str(L))).Value = 1
Worksheets("Sheet2").Range("C" & Trim(Str(L))).Value = Trim(Str(L))
Worksheets("Sheet2").Range("A" & Trim(Str(L))).Value = "PCT:" & Worksheets("Sheet1").Range("A" & Trim(Str(I))).Value
Worksheets("Sheet2").Range("B" & Trim(Str(L))).Value = "BT:" & Worksheets("Sheet1").Range("B" & Trim(Str(I))).Value
Next L
Next I
End Sub
All,
I am seeking information on how to change the default auto fill order from
ascending to descending when using alpha numeric entries.
To clarify, if you enter data into a cell and then click the lower right
corner and drag to the next lower cell(s) the value increases. I would like
it to decrease. How can I set that up.
I have inherited a spread sheet that has been edited by many people. One colum fails to sort when using the Data Filter options to sort Ascending or Desending. I have found that it is caused by the blanks in the colum. If I replace the blanks with numbers the sort works. Although I have solved the problem for now, I would like to understand what is causing it. Formatting for the cells is "General".
Can anyone supply some insight?
|
|