macro to sort left to right instead of up and down?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

I have a range of 10 rows

AA11:AZ21

AA11 :AA21 is the headers, with the data going along the rows.
I want to do a data sort macro that will sort the ten rows right to left (first alphabetically to last)
now my ideal situation would be this
I have 20 small shapes as buttons each call "ONbt" or "OFFbt" (With a number 1 to 10 so first is "Onbt1"

now if we select an "ONbt" button then it sorts by that row, then if i press a second "ONbt" it sorts by that row also, and so on.
pressing any OFFbt will clear all sorts. (but i can do that part :) )

So if the above is not possible or two complex just the code to sort left to right will be a big help

Thanks

Tony
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
This could help you.

Call the code with this line changing the range passed to the procedure as appropriate:

Call subSortRightToLeft(Worksheets("PO Data").Range("AB11:AK21"))

VBA Code:
Private Sub subSortRightToLeft(rngRangeToSort As Range)
Dim WsHelper As Worksheet
Dim rngRow As Range
        
    Set WsHelper = Worksheets("Helper")
    WsHelper.cells.clear
    WsHelper.range("A1")="Helper"
       
    For Each rngRow In rngRangeToSort.Rows
        
        WsHelper.Range("A2:A" & rngRow.Columns.Count + 1).Value = Application.Transpose(Application.Transpose(Application.Transpose(rngRow)))
        
        WsHelper.Range("A1:A" & rngRow.Columns.Count + 1).Sort Key1:=WsHelper.Range("A1"), Order1:=xlDescending, Header:=xlYes
                
        rngRow.Value = Application.Transpose(WsHelper.Range("A2:A" & rngRow.Columns.Count + 1))
    
    Next rngRow
        
End Sub
 
Upvote 0
So if the above is not possible or two complex just the code to sort left to right will be a big help
VBA Code:
Sub SORTIT()
    Dim i As Long
    Application.ScreenUpdating = False
    ActiveSheet.Sort.SortFields.Clear
    For i = 11 To 21
        Range(Cells(i, 28), Cells(i, 52)).Sort Key1:=Cells(i, 28), Order1:=xlAscending, Header:=xlNo, _
                                                 OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
    Next
    Application.ScreenUpdating = True
End Sub

Edit: added the Sortfields.Clear line
 
Last edited:
Upvote 0
To assign it to a shapes text you could do something like....
VBA Code:
Sub SORTIT()
    Dim i As String
    Application.ScreenUpdating = False
    ActiveSheet.Sort.SortFields.Clear
    i = ActiveSheet.Shapes("Rectangle 1").TextFrame2.TextRange.Characters.Text
        Range(Cells(i, 28), Cells(i, 52)).Sort Key1:=Cells(i, 28), Order1:=xlAscending, Header:=xlNo, _
                                                 OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
    Application.ScreenUpdating = True
End Sub

but you would have to do a separate code for each shape
 
Upvote 0
The post title suggests that a left to right sort is needed but the post text suggests right to left.

Both your answers are interesting MARK858 and I will look at the code later but I think that a right to left sort is required.

So strange that Microsoft did not think of having an xlRightToLeft sort option.

I forgot to add TonyWatsonHelp that you will need to add a worksheet named 'Helper' or whatever you want to call it but you
would need to change the code if it was named anything else.
 
Upvote 0
So strange that Microsoft did not think of having an xlRightToLeft sort option.
You just change xlAscending to xlDescending in the code.
I think it is Left to Right as he states "
(first alphabetically to last)".
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,204
Members
449,072
Latest member
DW Draft

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