?
Solved

Sorting Spreadsheet Issue

Posted on 2015-02-10
4
Medium Priority
?
46 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
[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
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 31

Accepted Solution

by:
gowflow earned 2000 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 49

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

770 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