Link to home
Start Free TrialLog in
Avatar of Patrick O'Dea
Patrick O'DeaFlag for Ireland

asked on

Excel - Sort left to right (or How to replace 20 macros with ONE)

Hi,

See attached.

In each cell from A2 to A20 I want a sort button.
I have put a sort button on A3 and A5 as an example.

What clever thing can I do to to do this 20 times.

In the first instance, I am not bothered by the actual buttons being added.
My primary requirement is to have one NEAT macro instead of repeating the same one 20 times.
SoftLefttoRight.xlsm
Avatar of regmigrant
regmigrant
Flag of United Kingdom of Great Britain and Northern Ireland image

The absolute simplest way I can think of would be to reformat so that the rows headers you want to sort are in the columns and the column headings are in the row (see attached). Then you can use the autofilter command which has a 'sort' in the drop down.

If you really, really want to do this with a button and a macro solution then you will need to identify the row from the button pressed and use that to calculate which row to use for the range. This will be difficult to administer if rows are added or deleted or someone sorts the table without using your buttons/macros, A better way would be to have a single sort button and have the user select which row they want before pressing sort and you can use the select to decide which row you are trying to use.

Let me know if the attached doesn't do it for you

Reg
Copy-of-SoftLefttoRight.xlsm
Here is another approach. The sort will occur if you double-click in any cell in column A below row 2. Enter this code in the worksheet module

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column = 1 And Target.Row > 2 Then
        Range("A1").CurrentRegion.Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Target.Resize(, 5), _
            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Selection
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
        Cancel = True
    End If
End Sub

Open in new window

For the button method you can select the row before running the macro and use the key value of

activecell
Saqib - that's leet
Theenks
Avatar of Patrick O'Dea

ASKER

Okay, Saqib your solution looks very elegant - just what I am looking for!

Except .... I can't get it to work!

I may be placing the code in the wrong area or something similar.

Any chance you could take my original spreadsheet and paste you code in the right place??

Thanks again for your help.
I've requested that this question be closed as follows:

Accepted answer: 350 points for ssaqibh's comment #a39803688
Assisted answer: 150 points for regmigrant's comment #a39803674
Assisted answer: 0 points for 21Dewsbury's comment #a39804201

for the following reason:

Saqib - Lovely elegant solution!
ASKER CERTIFIED SOLUTION
Avatar of Saqib Husain
Saqib Husain
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please excuse my typo earlier!

Thanks again!
Did you not intended to split the points between me and reg?
I'll give Reg an added bonus next time! :-)