星期日, 十二月 11, 2011

Copy Data to Another Worksheet with Advanced Filter

From Excel Semi-Pro
Copy Data to Another Worksheet with Advanced Filter:

Dear Microsoft, please fix the Advanced Filter in Excel. Copying filtered data to another worksheet should be simple but it’s not. Fixing this would help out many of your beginning and intermediate users and some of us old dogs who seemingly haven’t learned the trick.


The Problem with Advanced Filter in Excel


I was reading a post on the Contextures Blog, which indicated that you could use the Advanced Filter feature in Excel to copy filtered data to another worksheet. This feature has the potential to shorten some of my VBA code that essentially does the same thing. I mean, using Excel to do the work is simpler and usually faster than writing the code yourself.


So I created a simple worksheet with dummy data and tried out the Advanced Filter feature. In the picture below my List Range and Criteria Range are on Sheet1 and I’m just about to Copy to another location: Sheet2.


Advanced Filter Copy to Another Sheet


However, when I click the OK button the following message pops up.


Advanced Filter Warning


The Trick to Copy Filtered Data to Another Worksheet


My problem was that I didn’t know the secret. You have to initiate the Advanced Filter from the worksheet you are going to copy to. Duh, stupid me. So here’s how I defeated my nemesis, the Advanced Filter.



  1. Activate sheet 2 (where the data goes)

  2. Choose Data > Advanced Filter

  3. Select the List Range on sheet 1

  4. Select the Criteria range on sheet 1

  5. Select the radio button: Copy to another location

  6. Select the Copy to range on sheet 2

  7. Click OK


Here is the resulting data copied to Sheet 2.


Advanced Filter Results


There is a short video from Debra Dalgleish on how this is done. I’m too frustrated with Microsoft to pull off a video without disdain in my voice.


The problem I’m having is that this “work around” has been required for quite some time, with no updating of the Excel software code by Microsoft. (The video link above is for Excel 2003.) And yet in the latest two versions of Excel, 2010 and 2011, we still have to do something, that seems to me to be, completely counter-intuitive and un-productive.


Advanced Filter with VBA


I turned on the macro recorder to document the code required to execute, what I’m calling a “work around.” I then copied the first macro, modified the code, and ran it again from sheet 1 and it worked just fine.


Advanced Filter VBA Code


The first macro: CopyToOtherSheet does two things. 1) Selects Sheet 2 because that is what Microsoft requires, then 2) uses the AdvancedFilter method of the Range property to FilterCopy the CriteriaRange to the CopyToRange, with no Unique records.


The second macro: FilterCopyToOtherSheets only does one thing. It does what Microsoft should have done long ago, and that’s to copy the filtered range (sheet 1) to the copy range (sheet 2) while your on sheet 1.


The only code I changed (highlighted in red) was to eliminate the selection of Sheet 2, and add Sheets(“Sheet2″). to the CopyToRange. You can run this macro from Sheet 1 and the Advanced Filter will copy the filtered data to Sheet 2 with no problems.


Of course with static ranges this is a very limited macro, but my point here is that the code change should be minimal and if Microsoft can spend several billion dollars to get Windows 7 into Nokia phones, this shouldn’t cost to much to fix.


Okay, I’m done ranting.


Helpful Links


Excel’s Advanced Filter Help Online




没有评论: