PC Review


Reply
Thread Tools Rate Thread

Assign range to variable for sort

 
 
Michael Conroy
Guest
Posts: n/a
 
      11th Nov 2009
I am trying to do a unique sort to another location and I recorded this code.

Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A2:A69").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
"AA1"), Unique:=True

The next time there might be more than sixty-seven rows of data, so I did
this, which does not work.

Dim TargetArea as Range
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Set TargetArea = Selection.ActiveCell
Range([TargetArea]).... the rest of the sort code

How does one grab a range of cells, assign it to a variable, and refer to it
further down in the code? If this is not the proper way to do it, please feel
free to let me know. And thanks in advance for any help on this.

--
Michael Conroy
Stamford, CT
 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      11th Nov 2009
Hi Michael,

My first question is why start from A2? If you do not include the column
header in an Advanced filter/copy/unique then the first value is used as the
header in the output and you will actually see 2 elements the same. The
header and it will be repeated further down your list.

Having said that, is there any reason you cannot use the column for the
Advance filter range?

Example: (Note use of Columns in lieu of Range.)

Columns("A:A").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
"AA1"), Unique:=True

If you start getting errors when the code is changed because the header is
incorrect then delete all of the data in the Copy To range and also maybe the
Defined name 'Extract'. (Look up defined names in Help if you don't know how
to do this.)

--
Regards,

OssieMac


"Michael Conroy" wrote:

> I am trying to do a unique sort to another location and I recorded this code.
>
> Range("A2").Select
> Range(Selection, Selection.End(xlDown)).Select
> Range("A2:A69").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range( _
> "AA1"), Unique:=True
>
> The next time there might be more than sixty-seven rows of data, so I did
> this, which does not work.
>
> Dim TargetArea as Range
> Range("A2").Select
> Range(Selection, Selection.End(xlDown)).Select
> Set TargetArea = Selection.ActiveCell
> Range([TargetArea]).... the rest of the sort code
>
> How does one grab a range of cells, assign it to a variable, and refer to it
> further down in the code? If this is not the proper way to do it, please feel
> free to let me know. And thanks in advance for any help on this.
>
> --
> Michael Conroy
> Stamford, CT

 
Reply With Quote
 
OssieMac
Guest
Posts: n/a
 
      11th Nov 2009
Hello again Michael,

Another thing you should do when using Advanced filter with code is to clear
the old extracted data before re-running the code. If you don't do this
before you re-run the code and there are actually less elements in the
extracted data there was previously then you will still have extra data at
the bottom of the list from the previous run.

Because Excel creates a Defined name "Extract" for the position of the
column header of the extracted range, you can use it to identify the range to
be cleared.

Example: (Insert immediately before the advanced filter code line)
Range("Extract", Range("Extract").End(xlDown)).ClearContents

Note: You cannot insert that line in your code until after the advanced
filter has been run at least once.

--
Regards,

OssieMac


 
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
assign variable to a range Alberto Ast Microsoft Excel Programming 1 23rd Oct 2009 03:01 AM
Assign copied range to a variable johnmasvou Microsoft Excel Programming 3 20th Mar 2009 10:44 AM
VBA Excel how to assign name in variable range Mouimet Microsoft Excel Programming 4 14th Nov 2008 05:56 PM
range variable won't assign (chartobject.topleftcell property) Matthew Dodds Microsoft Excel Programming 2 16th Nov 2005 02:25 PM
How to assign a variable in a range select Paul Microsoft Excel Programming 5 3rd Jun 2005 11:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:52 PM.