• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macro to sort Worksheet by cell reference text

mayhem

New Member
Dear Chandoo you genius you, pls advise if possible to sort multiple worksheets by a cell reference text using a Macro. I am also curious to know if possible for cell reference by colour. Thank you
 
Mayhem


Firstly, Welcome to the Chandoo.org Forums


You may want to try the following VBA code:

[pre]
Code:
Sub SortSheets()
Dim lCount As Long, lCounted As Long
Dim lShtLast As Long
Dim lReply As Long
Dim SortCell As String
SortCell = "$A$1" 'Change address to suit your application

lReply = MsgBox("To sort Worksheets in Ascending order, according to Cell " + SortCell + _
", Select 'Yes'. " + Chr(13) + "To sort Worksheets in Descending order select 'No'", vbYesNoCancel, "Ozgrid Sheet Sort")

If lReply = vbCancel Then Exit Sub

lShtLast = Sheets.Count
If lReply = vbYes Then 'Sort ascending
For lCount = 1 To lShtLast
For lCount2 = lCount To lShtLast
If UCase(Sheets(lCount2).Range(SortCell).Value) < UCase(Sheets(lCount).Range(SortCell).Value) Then
Sheets(lCount2).Move Before:=Sheets(lCount)
End If
Next lCount2
Next lCount
Else 'Sort descending
For lCount = 1 To lShtLast
For lCount2 = lCount To lShtLast
If UCase(Sheets(lCount2).Range(SortCell).Value) > UCase(Sheets(lCount).Range(SortCell).Value) Then
Sheets(lCount2).Move Before:=Sheets(lCount)
End If
Next lCount2
Next lCount
End If

End Sub
[/pre]

Code was adapted from: http://www.ozgrid.com/VBA/sort-sheets.htm
 
Back
Top