Filtering and copying a sheet to another sheet... not working as planned!

Jed Shields

Active Member
Joined
Sep 7, 2011
Messages
283
Office Version
  1. 365
Platform
  1. Windows
Morning all,

I'm trying to filter a sheet and then copy the filtered data to another sheet. What I've got below works but it balloons the size of the workbook up to 180Mb! I'm assuming it's because I'm copying the entire columns rather than just the filtered data...?

Any thoughts on a better approach?

Code:
Sub CopyTIData()

Dim LastRow As Long

 LastRow = Range("AJ" & Rows.Count).End(xlUp).Row

    ActiveSheet.Range("A1:BI" & LastRow).AutoFilter Field:=37, Criteria1:= _
        "T&I IT"


    Range("A:BI").Select
    
    Selection.Copy
    
    Sheets("T&I Data").Select
    
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
        
 Sheets("Sheet1").Select
 
 ActiveSheet.ShowAllData
        
End Sub


Cheers,

Jed
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hello Jed,

Try the code amended as follows:-

Code:
Sub CopyTIData()

        Dim ws As Worksheet, sh As Worksheet
        Set ws = ActiveSheet
        Set sh = Sheets("T&I Data")
        
Application.ScreenUpdating = False

With ws.[A1].CurrentRegion
       .AutoFilter 37, "T&I IT"
       .Offset(1).EntireRow.Copy
       sh.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteAll
       .AutoFilter
End With

Application.CutCopyMode = False
Application.ScreenUpdating = True
        
End Sub

Test it in a copy of your workbook first.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
After filter you can try simple

#
Sheets(?).Cells.Copy
or
Sheets(?).Cells(1,1).CurrentRegion.Copy

Sheets("T&I Data").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
#

PS you have paste twice in your code
 
Upvote 0
Perfect! I'd managed to get my version working better, but it was still adding another 10Mb to the workbook, so obviously copying additional rows as well.

Quick question... how do I modify the code to filter 2 columns? I'm copying the code for another sheet that also needs column 57 as "Vacant".

Cheers

Jed
 
Upvote 0
Sheets(?).Columns("A:B").AutoFilter

You could try index column, then "hide" based on condition, and "copy visible only"

You may have one cell or one format somewhere, that is why when you copy it is copying 1,000,000 rows...
 
Upvote 0
Nikio8, your reply came in the same time as I was replying to vcooli. I haven't taken a look at your suggestions yet... :)

Vcoolio - how do I change your code from one filter to two filters?

Hello Jed,

Try the code amended as follows:-

Code:
Sub CopyTIData()

        Dim ws As Worksheet, sh As Worksheet
        Set ws = ActiveSheet
        Set sh = Sheets("T&I Data")
        
Application.ScreenUpdating = False

With ws.[A1].CurrentRegion
       .AutoFilter 37, "T&I IT"
       .Offset(1).EntireRow.Copy
       sh.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteAll
       .AutoFilter
End With

Application.CutCopyMode = False
Application.ScreenUpdating = True
        
End Sub

Test it in a copy of your workbook first.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Try adding this line
Code:
With Ws.[a1].CurrentRegion
       .AutoFilter 37, "T&I IT"
      [COLOR=#ff0000] .AutoFilter 57, "vacant"[/COLOR]
       .Offset(1).EntireRow.Copy
       sh.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlPasteAll
       .AutoFilter
End With
 
Upvote 0
Yep, just got it to work, along with filtering everything except T&I IT :)

Code:
With ws.[A1].CurrentRegion
       .AutoFilter 37, "<>T&I IT"
       .AutoFilter 57, "Vacant"
       .Offset(0).EntireRow.Copy
       sh.Range("A" & Rows.Count).End(3)(1).PasteSpecial xlPasteAll
       .AutoFilter
End With
 
Upvote 0
Hello Jed,

I'm glad that its working for you but I don't follow. In Column 37, you now want to filter everything that is not equal to "T & I IT"?

If you want to keep your file to around the same size after each transfer, you may want to look at deleting the filtered rows from the active sheet once each transfer is completed.

@ Fluff:
Thanks for dropping in that extra line of code in post #7 . I would have been fast asleep when you did that!

Cheerio.
vcoolio.
 
Upvote 0
Sorry for the confusion vcoolio, I'm actually filtering 3 different lists to 3 seperate sheets, one of them is =T&I IT, the other 2 are <>T&I IT, plus an additional filter of =VACANT and =FILLED in column 57. It' all working now, although I haven't looked at the extra code in post 7 yet.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top