Solved

Excel VBA - Sort left to Right (But not handling header correctly)

Posted on 2014-01-23
3
549 Views
Last Modified: 2014-01-23
Hi,

This is a follow-on question from one I raised earlier (which was resolved).

See attached.
Note that if you double-click on column "A" this will result in a sorting of the numbers in ascending sequence.  (Left to Right)

However, it ALSO sorts the header which I do not want to happen.

The VBA would appear to indicate that a header is present - so WHY is the header included in the sort.

Thanks!
SortLeftToRight.xlsm
0
Comment
Question by:Patrick O'Dea
3 Comments
 
LVL 20

Expert Comment

by:CompProbSolv
ID: 39805059
I noticed that when one tries to do the sort manually in Excel, if you select left-to-right then the box for "My Data has Headers" is greyed out.  I would assume that the two are mutually exclusive.

I would leave the headers out of the selection.
0
 
LVL 32

Accepted Solution

by:
Robberbaron (robr) earned 500 total points
ID: 39805197
try this. it resizes the sortzone, and also removes hardcoded extents.

Private Sub Worksheet_BeforedoubleClick(ByVal Target As Range, Cancel As Boolean)
    Dim sortzone As Range, thisSheet As Worksheet
    If Target.Column = 1 And Target.Row > 1 Then
        Set thisSheet = ActiveWorkbook.ActiveSheet
        Set sortzone = thisSheet.Range("A1").CurrentRegion.Offset(0, 1)
        Set sortzone = sortzone.Resize(, sortzone.Columns.Count - 1)
        
        thisSheet.Sort.SortFields.Clear
        thisSheet.Sort.SortFields.Add Key:=Target.Offset(0, 1).Resize(, sortzone.Columns.Count), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With thisSheet.Sort
            .SetRange sortzone
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
        Cancel = True
    End If
    Range("A1").Select
End Sub

Open in new window

0
 

Author Closing Comment

by:Patrick O'Dea
ID: 39805236
One word only!

Perfect!
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

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,…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

947 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

20 Experts available now in Live!

Get 1:1 Help Now