Solved

Excel 2007 VBA - Sorting Dynamic Ranges

Posted on 2011-02-26
3
1,890 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
  • 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

Free book by J.Peter Bruzzese, Microsoft MVP

Are you using Office 365? Trying to set up email signatures but you’re struggling with transport rules and connectors? Let renowned Microsoft MVP J.Peter Bruzzese show you how in this exclusive e-book on Office 365 email signatures. Better yet, it’s free!

Join & Write a Comment

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video discusses moving either the default database or any database to a new volume.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now