Macro for horizontal sorting of "selected cells"

daw60

New Member
Joined
Jan 26, 2015
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
Would like to be able to select cells horizontally and be able to sort only those cells selected. Would work just like the sort A-Z(↓) button except it works Left to Right. I would then just create a button and attach the macro to it.

Thanks for any help.

<tbody>
</tbody>
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
daw60,

Would like to be able to select cells horizontally and be able to sort only those cells selected.

You might consider the following...

Code:
Sub HorizontallySortArray_1017900()
''''Sorts a single row, left to right
''''Do not select an entire row

Dim arr As Variant
Dim rng As Range
Dim x As Long, y As Long
Dim Txt1 As String, Txt2 As String

Set rng = Selection
arr = rng
For x = LBound(arr, 2) To UBound(arr, 2)
    For y = x To UBound(arr, 2)
        If UCase(arr(1, y)) < UCase(arr(1, x)) Then
          Txt1 = arr(1, x)
          Txt2 = arr(1, y)
          arr(1, x) = Txt2
          arr(1, y) = Txt1
      End If
     Next y
Next x
rng.Value = arr
End Sub



Cheers,

tonyyy
 
Last edited:
Upvote 0
Why not use Excel's built-in sort tool (which can cover more than one row selection and allows entire row selection)?
The code can be obtained via the macro recorder and tweaked to change the hard-coded ranges to Selection.
 
Upvote 0
Maybe this:


Please use Code Tags when posting a code. Like this: [CODE ]Your Code Here[/ CODE]
Code:
[B][COLOR=Royalblue]Sub[/COLOR][/B] a1017900a[B]()[/B]
      [B][COLOR=Royalblue]With[/COLOR][/B] Selection
          [B].[/B]Sort Key1[B]:=.[/B]Cells[B]([/B][B][B][COLOR=crimson]1[/COLOR][/B][/B][B]),[/B] Order1[B]:=[/B]xlAscending[B],[/B] Orientation[B]:=[/B]xlLeftToRight[B],[/B] Header[B]:=[/B]xlNo[B],[/B] MatchCase[B]:=[/B][B][COLOR=Royalblue]False[/COLOR][/B]
      [B][COLOR=Royalblue]End[/COLOR][/B] [B][COLOR=Royalblue]With[/COLOR][/B]
 
[B][COLOR=Royalblue]End[/COLOR][/B] [B][COLOR=Royalblue]Sub[/COLOR][/B]

 
Upvote 0
Maybe this:


Please use Code Tags when posting a code. Like this: [CODE ]Your Code Here[/ CODE]
Code:
[B][COLOR=Royalblue]Sub[/COLOR][/B] a1017900a[B]()[/B]
      [B][COLOR=Royalblue]With[/COLOR][/B] Selection
          [B].[/B]Sort Key1[B]:=.[/B]Cells[B]([/B][B][B][COLOR=crimson]1[/COLOR][/B][/B][B]),[/B] Order1[B]:=[/B]xlAscending[B],[/B] Orientation[B]:=[/B]xlLeftToRight[B],[/B] Header[B]:=[/B]xlNo[B],[/B] MatchCase[B]:=[/B][B][COLOR=Royalblue]False[/COLOR][/B]
      [B][COLOR=Royalblue]End[/COLOR][/B] [B][COLOR=Royalblue]With[/COLOR][/B]
 
[B][COLOR=Royalblue]End[/COLOR][/B] [B][COLOR=Royalblue]Sub[/COLOR][/B]

[Very simple and Works perfectly...Thank you for helping]
 
Upvote 0
You're welcome & thanks for replying
 
Upvote 0

Forum statistics

Threads
1,216,474
Messages
6,130,841
Members
449,598
Latest member
sunny_ksy

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