Solved

Excel 2007 VBA - Sorting Dynamic Ranges

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

816 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

8 Experts available now in Live!

Get 1:1 Help Now