Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel 2007 VBA - Sorting Dynamic Ranges

Posted on 2011-02-26
3
Medium Priority
?
2,187 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
A couple of months ago we ran into an issue that necessitated re-creating our Edge Subscriptions. However, when we attempted to execute the command: New-EdgeSubscription -filename C:\NewEdgeSub_01.xml we received an error indicating that the LDAP se…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

722 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