Solved

Excel 2007 VBA - Sorting Dynamic Ranges

Posted on 2011-02-26
3
2,126 Views
Last Modified: 2012-05-11
How can I sort a dynamic range using vba?

Dynamic range "Td_MyDynamicRange" will ALWAYS include in its header cells "c1_NamedHdrCell" and "c2_NamedHdrCell" named ranges

I want to sort by the two columns of [c1_NamedHdrCell] and [c2_NamedHdrCell]

Thanks, --Andres
0
Comment
Question by:AndresHernando
[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
  • 2
3 Comments
 
LVL 45

Accepted Solution

by:
patrickab earned 500 total points
ID: 34986917
AndresHernando,

The VBA below is in the attached file.

The range 'rng' is dynamic and will adjust to fit the data in columns A & B.

Patrick
Sub sorter()
Dim rng As Range

With Sheets("Sheet1")
    Set rng = Range(.Cells(1, "A"), .Cells(.Rows.Count, "B").End(xlUp))
End With

rng.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("B1") _
    , Order2:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:= _
    False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
    :=xlSortNormal
    
End Sub

Open in new window

sorting-dynamic-range-01.xls
0
 

Author Closing Comment

by:AndresHernando
ID: 34987685
Thanks!  --Andres
0
 
LVL 45

Expert Comment

by:patrickab
ID: 34987866
Andres - Thanks for the grade - Patrick
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

In-place Upgrading Dirsync to Azure AD Connect
This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Suggested Courses

624 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