Filter Data and Copy Multiple Sections

reeser

Board Regular
Joined
May 19, 2006
Messages
215
I have a list that I'd like to filter on Field 3 and copy the visible cells starting in A2 to column G then paste into Sheet2 A2. Then copy visible cells from J2 to M and paste into Sheet2 H2. Previously I was using this code for just the first part but I'm having trouble modifying it to handle copy/paste of the second section (J2-M). Any suggestions?

Code:
Sub Filter_NWSheet()

Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnStart As Range, rnData As Range

Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Sheet1")

With wsSheet
Set rnStart = .Range("A1")
Set rnData = .Range(.Range("A2"), .Cells(.Rows.Count, 7).End(xlUp))

End With

Application.ScreenUpdating = True

rnStart.AutoFilter Field:=3, Criteria1:="x"

rnData.SpecialCells(xlCellTypeVisible).Copy
Sheets("Sheet2").Range("A2").PasteSpecial xlPasteValues


rnStart.AutoFilter Field:=3

With Application
.CutCopyMode = False
.ScreenUpdating = False
End With

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
It's not very elegant but will adding the following code
Code:
With wsSheet
Set rnData = .Range(.Range("J2"), .Cells(.Rows.Count, 4).End(xlUp))
End With

rnData.SpecialCells(xlCellTypeVisible).Copy
Sheets("Sheet2").Range("H2").PasteSpecial xlPasteValues

after
Code:
Sheets("Sheet2").Range("A2").PasteSpecial xlPasteValues
do it?


If you need to perform this task reqularly, an alternative that you may wish to explore - though this will require some structural changes to your workbook to add a criteria range and different code - is to use the Advanced Filter funtionality, as this will allow you to copy all the data you require in a single step.

HTH
 
Upvote 0
Another example:

Code:
[COLOR=darkblue]Sub[/COLOR] Filter_NWSheet()
    
    Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
    [COLOR=darkblue]With[/COLOR] ThisWorkbook.Worksheets("Sheet1")
        .Range("A1").AutoFilter Field:=3, Criteria1:="x"
        .Range("A2", .Cells(.Rows.Count, "G").End(xlUp)).Copy
        Sheets("Sheet2").Range("A2").PasteSpecial xlPasteValues
        .Range("J2", .Cells(.Rows.Count, "M").End(xlUp)).Copy
        Sheets("Sheet2").Range("H2").PasteSpecial xlPasteValues
        .Range("A1").AutoFilter Field:=3
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    [COLOR=darkblue]With[/COLOR] Application
        .CutCopyMode = [COLOR=darkblue]False[/COLOR]
        .ScreenUpdating = [COLOR=darkblue]True[/COLOR]
    [COLOR=darkblue]End[/COLOR] With
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Last edited:
Upvote 0
Another example:

Code:
[COLOR=darkblue]Sub[/COLOR] Filter_NWSheet()
    
    Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
    [COLOR=darkblue]With[/COLOR] ThisWorkbook.Worksheets("Sheet1")
        .Range("A1").AutoFilter Field:=3, Criteria1:="x"
        .Range("A2", .Cells(.Rows.Count, "G").End(xlUp)).Copy
        Sheets("Sheet2").Range("A2").PasteSpecial xlPasteValues
        .Range("J2", .Cells(.Rows.Count, "M").End(xlUp)).Copy
        Sheets("Sheet2").Range("H2").PasteSpecial xlPasteValues
        .Range("A1").AutoFilter Field:=3
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    [COLOR=darkblue]With[/COLOR] Application
        .CutCopyMode = [COLOR=darkblue]False[/COLOR]
        .ScreenUpdating = [COLOR=darkblue]True[/COLOR]
    [COLOR=darkblue]End[/COLOR] With
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

That is perfect. Thanks for taking a look at it and coming up with a solution.
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,644
Members
449,461
Latest member
kokoanutt

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