Solved

Excel VBA sorting multiple columns with headers

Posted on 2011-03-25
8
4,944 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

739 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