Solved

Sorting Spreadsheet Issue

Posted on 2015-02-10
4
40 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

776 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