Solved

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

Posted on 2014-01-23
11
266 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Highlighting cells in Excel 9 17
Excel 2016 formulas 5 30
Message Logon Windows 2012 R2 Server 16 15
Dynamic Filter ? 4 21
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

896 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

14 Experts available now in Live!

Get 1:1 Help Now