Solved

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

Posted on 2014-01-23
11
254 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
  • 5
  • 4
  • 2
11 Comments
 
LVL 19

Expert Comment

by:regmigrant
Comment Utility
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
Comment Utility
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
Comment Utility
For the button method you can select the row before running the macro and use the key value of

activecell
0
 
LVL 19

Expert Comment

by:regmigrant
Comment Utility
Saqib - that's leet
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
Comment Utility
Theenks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Patrick O'Dea
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Please excuse my typo earlier!

Thanks again!
0
 
LVL 43

Expert Comment

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

Author Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now