Solved

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

Posted on 2014-01-23
11
291 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

732 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