PC Review


Reply
Thread Tools Rate Thread

Creating a macro to sort a complex dynamic named reference

 
 
Dan
Guest
Posts: n/a
 
      5th Sep 2008
Hi,
I'm having trouble creating a macro that will sort a named reference. I have
data in Columns A-D that will constantly be adding rows at the bottom. I have
4 header rows at the top so the data does not start until A5. I have created
a named reference called "Totals" which looks like this:
"=OFFSET(Totals!$A$5,0,0,COUNTA(Totals!$A:$A)-3,4)"

I then tried the code below to sort it but am getting an error: "The sort
reference is not valid. Make sure that it's within the data you want to sort,
and the first Sort By blow isn't the same or blank."

Application.Goto Reference:="Totals"
Selection.Sort Key1:=Range("A5"), Order1:=xlAscending, Key2:=Range("C5") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom

I've tried searching but so far haven't found any solutions for using named
references instead of defined blocks of cells for this problem.

Any help would be appreciated!

Thank you,
-Dan
 
Reply With Quote
 
 
 
 
Jim Thomlinson
Guest
Posts: n/a
 
      5th Sep 2008
Here is how I would do it...

Dim rngToSort As Range
Set rngToSort = ThisWorkbook.Names("Totals").RefersToRange

With rngToSort
.Sort Key1:=.Parent.Range("A5"), order1:=xlAscending, _
Key2:=.Parent.Range("C5"), order1:=xlAscending, _
Header:=xlNo
End With

--
HTH...

Jim Thomlinson


"Dan" wrote:

> Hi,
> I'm having trouble creating a macro that will sort a named reference. I have
> data in Columns A-D that will constantly be adding rows at the bottom. I have
> 4 header rows at the top so the data does not start until A5. I have created
> a named reference called "Totals" which looks like this:
> "=OFFSET(Totals!$A$5,0,0,COUNTA(Totals!$A:$A)-3,4)"
>
> I then tried the code below to sort it but am getting an error: "The sort
> reference is not valid. Make sure that it's within the data you want to sort,
> and the first Sort By blow isn't the same or blank."
>
> Application.Goto Reference:="Totals"
> Selection.Sort Key1:=Range("A5"), Order1:=xlAscending, Key2:=Range("C5") _
> , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
> False, Orientation:=xlTopToBottom
>
> I've tried searching but so far haven't found any solutions for using named
> references instead of defined blocks of cells for this problem.
>
> Any help would be appreciated!
>
> Thank you,
> -Dan

 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      8th Sep 2008
Hi Jim,

Unfortunately I still get a runtime 1004 - "Application-defined or
object-defined error". My code looks like this:

Sub SortTotals()

Dim rngToSort As Range
Set rngToSort = ThisWorkbook.Names("Totals").RefersToRange

With rngToSort
..Sort Key1:=.Parent.Range("A5"), order1:=xlAscending, _
Key2:=.Parent.Range("C5"), order1:=xlAscending, _
Header:=xlNo
End With

End Sub

Any ideas on what might be causing this?
-Dan


"Jim Thomlinson" wrote:

> Here is how I would do it...
>
> Dim rngToSort As Range
> Set rngToSort = ThisWorkbook.Names("Totals").RefersToRange
>
> With rngToSort
> .Sort Key1:=.Parent.Range("A5"), order1:=xlAscending, _
> Key2:=.Parent.Range("C5"), order1:=xlAscending, _
> Header:=xlNo
> End With
>
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Dan" wrote:
>
> > Hi,
> > I'm having trouble creating a macro that will sort a named reference. I have
> > data in Columns A-D that will constantly be adding rows at the bottom. I have
> > 4 header rows at the top so the data does not start until A5. I have created
> > a named reference called "Totals" which looks like this:
> > "=OFFSET(Totals!$A$5,0,0,COUNTA(Totals!$A:$A)-3,4)"
> >
> > I then tried the code below to sort it but am getting an error: "The sort
> > reference is not valid. Make sure that it's within the data you want to sort,
> > and the first Sort By blow isn't the same or blank."
> >
> > Application.Goto Reference:="Totals"
> > Selection.Sort Key1:=Range("A5"), Order1:=xlAscending, Key2:=Range("C5") _
> > , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
> > False, Orientation:=xlTopToBottom
> >
> > I've tried searching but so far haven't found any solutions for using named
> > references instead of defined blocks of cells for this problem.
> >
> > Any help would be appreciated!
> >
> > Thank you,
> > -Dan

 
Reply With Quote
 
Member
Join Date: Apr 2008
Posts: 66
 
      8th Sep 2008
The double dot in:
..Sort Key1:=.Parent.Range("A5")
looks like a possible culprit to me.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Large Dynamic Named Range Sort Problem CG Microsoft Excel Programming 3 15th Aug 2011 09:55 PM
Creating dynamic ranges which are named. U0107 Microsoft Excel Worksheet Functions 1 5th Jan 2010 11:46 PM
Dynamic named range reference from external workbook vertblancrouge Microsoft Excel Misc 2 7th Aug 2009 05:07 PM
Creating a Dynamic Named Range Using Sheet Name and Column Header burl_rfc_h Microsoft Excel Programming 8 13th Feb 2006 10:53 PM
How to dynamically reference a dynamic named range paris3 Microsoft Excel Worksheet Functions 4 24th Jun 2005 01:22 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:16 AM.