Solved

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

Posted on 2014-01-23
3
543 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

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

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

708 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