Solved

Sorting Spreadsheet Issue

Posted on 2015-02-10
4
36 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:SimonAdept
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 45

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

757 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

18 Experts available now in Live!

Get 1:1 Help Now