Excel VBA: Filter, cut, and paste to another sheet

11392

New Member
Joined
Oct 20, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
1st sheet named src while the 2nd one is dst which is an empty sheet at the moment.

My plan is to filter string x in column B, cut it and paste it to 2nd sheet dst

enter image description here

VBA code

VBA Code:
Sub filter_copy_paste()

With Sheets("src")
    .Range("A1").AutoFilter Field:=2, Criteria1:="x"
    With .AutoFilter.Range
        With .SpecialCells(xlCellTypeVisible).EntireRow
            .Copy
            With Sheets("dst")
                .Paste
                .[A1].Select
            End With
        End With
    End With
End With

End Sub

enter image description here

However, there is an error when I run it and when I hit Debug, it highlights line number 5 which is With .AutoFilter.Range

What wrong in this code and what should I do to fix it?

Desired output in 1st sheet "src"

enter image description here

Desired output in 2nd sheet "dst"

enter image description here
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this.

Note: When the data is copied to the destination worksheet, we use the entire AutoFilter range as you did.
However, to remove the copied rows from the sheet (I assume you want them to be removed), then we should exclude the header row, so .Areas(2) is used for that purpose.

VBA Code:
Sub filter_copy_paste()
    With Sheets("src")
        .Range("A1").AutoFilter Field:=2, Criteria1:="x"
        With .AutoFilter.Range.SpecialCells(xlCellTypeVisible)
            With .EntireRow
                .Copy Sheets("dst").Range("A1")
            End With
            With .Areas(2).EntireRow
                .Delete (xlShiftUp)
            End With
        End With
        .Range("A1").AutoFilter
    End With
    Application.Goto Sheets("dst").Range("A1")
End Sub
 
Upvote 0
Just a slightly different method. Btw do either of the responses also answer your question

VBA Code:
Sub filter_copy_paste()

    With Sheets("src")
        .Range("A1").AutoFilter Field:=2, Criteria1:="x"
        With .AutoFilter.Range
            .EntireRow.Copy Sheets("dst").Range("A1")
            .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        End With
        .AutoFilterMode = False
    End With
    Application.Goto Sheets("dst").Range("A1")
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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