Solved

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

Posted on 2014-01-23
11
294 Views
Last Modified: 2014-01-24
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
0
Comment
Question by:Patrick O'Dea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 19

Expert Comment

by:regmigrant
ID: 39803674
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
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39803688
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

0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39803693
For the button method you can select the row before running the macro and use the key value of

activecell
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 19

Expert Comment

by:regmigrant
ID: 39803746
Saqib - that's leet
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39803969
Theenks
0
 

Author Comment

by:Patrick O'Dea
ID: 39804201
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.
0
 

Author Comment

by:Patrick O'Dea
ID: 39804280
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!
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 39804281
You are not supposed to select your own comment when accepting; unless there is a point to make.
0
 

Author Closing Comment

by:Patrick O'Dea
ID: 39804937
Please excuse my typo earlier!

Thanks again!
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39805572
Did you not intended to split the points between me and reg?
0
 

Author Comment

by:Patrick O'Dea
ID: 39805920
I'll give Reg an added bonus next time! :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question