+ Reply to Thread
Results 1 to 12 of 12

Thread: Autofiltering, copying cells and specialcells

  1. #1
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    97

    Autofiltering, copying cells and specialcells

    I have developed a macro to import, filter and analyse data sets. It works great on the data file I used to develop it, but when I take a different file, with the same format, it fails!

    I've used this code to filter the data, select the rows I want and paste them into another worksheet

    .Range("A1").Value = "code"
    Selection.AutoFilter
    Range("A1", ActiveCell.SpecialCells(xlLastCell)).AutoFilter Field:=1, Criteria1:="$GF3DW"
    Range("A1", ActiveCell.SpecialCells(xlLastCell)).Copy _ 
          Destination:=Worksheets("Sheet2").Range("A1")
    It works great, for the most part. But in this other data set all variables are copied to Sheet2, not just those left after the filter has been applied.

    I can't understand it , any ideas?

    Thanks
    Last edited by jlt199; 03-31-2010 at 07:45 PM.

  2. #2
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    97

    Re: Autofiltering, copying cells and specialcells

    Ok, I've tried recording a macro, doing what I want with the difficult dataset and get the error

    Microsoft Office Excel cannot create or use the data range reference because it is too complex. Try one of the following:
    • Use data that can be selected in one contiguous rectangle
    • Use data from the same sheet
    I guess I need a different method of achieving my goal - although I have no idea why this file is anymore complex than the one that works!

    Is there a better/more stable method of achieving my goal?

    Thanks

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Autofiltering, copying cells and specialcells

    Hello jlt199,

    The problem is how you are finding the last cell in Column "A". The SpecialCells(xlLastCell)) will not find the last cell with data. It will find the last cell that is formatted. Here is how to find the last cell with data,
    Range("A1").Value = "code"
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    Selection.AutoFilter
    Range("A1:A" & LastRow).AutoFilter Field:=1, Criteria1:="$GF3DW"
    Range("A1:A" & LastRow).Copy Destination:=Worksheets("Sheet2").Range("A1")
    Here is another way to code it...
    Dim Rng As Range
      ActiveSheet.AutoFilterMode = False
      Set Rng = Range("A1", Cells(Rows.Count, "A").End(xlUp))
      Rng.AutoFilter Field:=1, Criteria1:="$GF3DW"
      Rng.SpecialCells(xlCellTypeVisible).Copy Destination:=Worksheets("Sheet2").Range("A1")
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    97

    Re: Autofiltering, copying cells and specialcells

    Thanks for your reply Leith,

    I have tried replacing my code with yours, but I'm still getting the same error message. I believe it's because of the disjointed nature of the data I am trying to copy. Is there a less memory intensive, or complex, method of moving the result of the filter to a different workbook?

    Thanks

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Autofiltering, copying cells and specialcells

    Hello ,

    You could sort the data beforehand. That would make all the rows contiguous.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    97

    Re: Autofiltering, copying cells and specialcells

    I'm not sure if it will help you understand or not, but my data has over 25000 rows and I'm filtering out about half of it. Hence alot of the rows that have survived the filter were surrounded by rows that haven't, making the final data very disjointed in sequence. For example the first 20 rows to survive the cull are:

    2,4,6,7,9,11,12,14,16,17,19,21,22,24,26,27,29,31,32,34

    So I can understand why Excel finds this too complex to copy. Unfortunately what I don't understand is what to do about it!

  7. #7
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    97

    Re: Autofiltering, copying cells and specialcells

    Sorry, you replied whilst I was writing my last post!

    Sorting data is a good idea. I'll let you know...

  8. #8
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    523

    Re: Autofiltering, copying cells and specialcells

    Sort the data based on the column that you are filtering, so that all the data is still contiguous after you run your filter, then it shouold copy and paste without the error using Leith's code. You can add the sort code above your filter/copy code, so it is all accomplished programatically.

  9. #9
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    523

    Re: Autofiltering, copying cells and specialcells

    Sorry. You posted your response while I was typing MY reply.

    Have a good one.

  10. #10
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    97

    Re: Autofiltering, copying cells and specialcells

    Hutch, I can't work out if you are being ironic or criticising my grammar. Either way, thanks for your help

    I've managed to filter the data, which is preventing the error I was getting... However, this in turn has caused a new problem. I have to apply the filter twice, the rsult fo the first one gets copied to Sheet2 and the result of the second goes to Sheet3. I'm using this code

    Dim Rng As Range
                .Range("A1").Value = "code"
                
                .Sort.SortFields.Clear
                .Sort.SortFields.Add Key:=Range("A1") _
                    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                With .Sort
                    .SetRange Range("A2", Cells(Rows.Count, "L").End(xlUp))
                    .Header = xlNo
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
                
                ActiveSheet.AutoFilterMode = False
                Set Rng = Range("A1", Cells(Rows.Count, "L").End(xlUp))
                Rng.AutoFilter Field:=1, Criteria1:="$GF3DW"
                Rng.SpecialCells(xlCellTypeVisible).Copy _
                    Destination:=Worksheets("Sheet2").Range("A1")
                
                ActiveSheet.AutoFilterMode = False
                Rng.AutoFilter Field:=1, Criteria1:="$GFDTA"
                Rng.AutoFilter Field:=9, Criteria1:=1
                Rng.SpecialCells(xlCellTypeVisible).Copy _
                    Destination:=Worksheets("Sheet3").Range("A1")
    The code going to Sheet2 works great. But nothing is being copied to Sheet3 - I can see it is being filtered correctly, so t's something with the copy and paste. Does one of the parameters have to be reset after the first time, or is there another reason you can see?

    Also, my code for sorting the data looks a little ugly, is there a more elegent method of coding this?

    Thanks again

  11. #11
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    523

    Re: Autofiltering, copying cells and specialcells

    It wasn't making fun, just observing the ironic.

    The only issue I can see is that you appear to be filtering on two fields when you copy to sheet 3, but the syntax doesn't look corect. It looks like the 2nd filter would overwrite the first.

    I have two suggestions:
    1. Record the filter process again, and then study the results.
    2. Debug the macro by stepping through it step-by-step using F8, with your VB Editor and your Excel window both visible, and you can see what's happening in each step of your macro.

    Hope that helps.

  12. #12
    Registered User
    Join Date
    03-19-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    97

    Re: Autofiltering, copying cells and specialcells

    Thanks Hutch, I seem to have it working now. Although I admit I had to go back to my original syntax for the copying. Still not sure why.

    Thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0