Solved

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

Posted on 2014-01-23
3
583 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
[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
3 Comments
 
LVL 21

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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 …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

738 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