Solved

Sorting Spreadsheet Issue

Posted on 2015-02-10
4
42 Views
Last Modified: 2015-03-08
Hi,

I am sorting a spreadhseet by the value of the column A. The first 2 rows are the header. The following code shows an error in the fourth line (Run-time error '1004': Sort method of Range class failed), but if I change the range of the With in the third line to ("A3:AL10")instead of ("A3:AL100") it works properly. Any solution or advice to extend that range to ("A3:AL100") ?

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A3:A100")) Is Nothing Then
With Range("A3:AL100")
.Sort key1:=.Cells(1, 1)
End With
End If
End Sub
0
Comment
Question by:lnrivera
4 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40601554
I can't replicate your error. The code you posted works fine for me. Can you post the workbook?
0
 
LVL 29

Accepted Solution

by:
gowflow earned 500 total points
ID: 40602609
For sure you will get an error with a routine like this in an event like this and here is the reason:

You are performing a sort meaning that cells will invoke a change event in a Change event routine without any control on events. reason why you will endup with cascading events launched as a result of the sort which will ultimately cause an error. It is simply by luck that by changing the range you escaped the error. If you insist on doing the sort in the change event here is how to do it.

This is the code and below is a workbook sample you simply change any value in Col A and see the results.

Private Sub Worksheet_Change(ByVal Target As Range)

'---> Disable Events
With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

If Not Intersect(Target, Range("A3:A100")) Is Nothing Then
    With Range("A3:AL100")
        .Sort key1:=.Cells(1, 1), Header:=xlNo, MatchCase:=False
    End With
End If

'---> Enable Events
With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

End Sub

Open in new window


gowflow
Sort.xlsm
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40652266
I've requested that this question be closed as follows:

Accepted answer: 500 points for gowflow's comment #a40602609

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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