Solved

Excel 2007 VBA - Sorting Dynamic Ranges

Posted on 2011-02-26
3
2,063 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
how to add IIS SMTP to handle application/Scanner relays into office 365.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

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