Solved

Excel VBA sorting multiple columns with headers

Posted on 2011-03-25
8
4,700 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

861 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