Solved

Excel VBA sorting multiple columns with headers

Posted on 2011-03-25
8
5,202 Views
Last Modified: 2012-05-11
Hello,

I am struggling with sorting a range in Excel using VBA.  I have the following code:

dim wp as worksheet
set wp = worksheets("Parameters")
lastrow = wp.Range("Z" & Rows.Count).End(xlUp).Row

With wp
    .Sort.SortFields.Add Key:=Range( _
        "AA2:AA" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    .Sort.SortFields.Add Key:=Range( _
        "Z2:Z" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With .Sort
        .SetRange Range("Z2:AC" & lastrow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End With

Every time I try to sort I receive an error:
Run-time error '1004':
"The sort reference is not valid.  Make sure that it's within the data you want to sort, and the first sort by box isn't the same or blank"    

I'm sure this is just a simple code issue, but I'm stumped.  Can someone please help me ?  This code is just part of a much bigger program.
0
Comment
Question by:mat58
[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
  • 2
  • 2
8 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 150 total points
ID: 35218629
try using  this code
Do you have data all the way through column AC?

Thomas

dim wp as worksheet
set wp = worksheets("Parameters")
lastrow = wp.Range("Z" & Rows.Count).End(xlUp).Row

With wp
    .Range("Z2:AC" & lastrow).Sort.SortFields.Add Key:=Range( _
        "AA2:AA" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    .Range("Z2:AC" & lastrow).Sort.SortFields.Add Key:=Range( _
        "Z2:Z" & lastrow), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With .Sort
        .SetRange Range("Z2:AC" & lastrow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End With

Open in new window

0
 

Author Comment

by:mat58
ID: 35218903
I still receive an error:  "Unable to get the sort property of the Range class"
0
 
LVL 6

Assisted Solution

by:reitzen
reitzen earned 100 total points
ID: 35218990
Your code runs fine for me on 2007 & 2010.   Take a look at the Sort dialog from the Data ribbon and/or the data in your range for any clues to the error message

"...Make sure that it's within the data you want to sort, and the first sort by box isn't the same or blank"
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:mat58
ID: 35219156
The data range does have blank rows amongst it.   My overall goal is to sort is to compress the data and have the blanks at the bottom.   The first sort box (header) is not blank.  The blanks in the middle, is that causing the problem ?
0
 
LVL 6

Expert Comment

by:reitzen
ID: 35219234
I don't think the empty cells are the problem, and shouldn't be, by design.  I tried it with an empty cell in the first row of columns AA & Z, as well as with empty cells at the end and middle of the range.  Each time it sorted fine.

Could the values in the column headers (row 2) be words that might force this error?
0
 
LVL 39

Expert Comment

by:nutsch
ID: 35219236
Why not remove the blanks with autofilter instead?
0
 

Author Comment

by:mat58
ID: 35219241
I figured it out.  Because there is a number of sheet changes throughout the program, I just added the following prior to the sort:

Sheets("Paramaters").select
Range("Z:AC").select

Thanks for your help
0
 

Author Closing Comment

by:mat58
ID: 35226983
Forcing the select for the worksheet and the range resolved the problem.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
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 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.

617 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