Macro to filter and copy first 10 lines of data only

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I want to filter sheet "Data" Range A1:K & lastrow"
bfilter column F to show "P" only then copy this data into
Sheets "Diary" Range G8
But I only want to copy 10 rows max,
any ideas how i can do this
Thanks

Tony
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Easiest way would be to copy the filtered data & then delete row 11 onwards.
 
Upvote 0
try this

Code:
Sub Copy10RowsMax()
    Dim Ws1 As Worksheet, Cels As Range, Rng1 As Range, Rng2 As Range, r As Long, x As Long
    Set Ws1 = Sheets("Data")
    Set Rng1 = Ws1.Range("A1").CurrentRegion
    ShowAll Ws1
    Rng1.AutoFilter Field:=6, Criteria1:="P"
'max 10 rows to be copied
    For r = 2 To Rng1.Rows.Count
        If Not Ws1.Rows(r).EntireRow.Hidden Then
            x = x + 1
            Set Cels = Ws1.Cells(r, 1).Resize(, 11)
            If Rng2 Is Nothing Then Set Rng2 = Cels Else Set Rng2 = Union(Rng2, Cels)
            If x = 10 Then Exit For
        End If
    Next
'copy & paste
    Rng2.Copy
    With Sheets("Diary").Range("G8")
        .PasteSpecial (xlPasteFormats)
        .PasteSpecial (xlPasteValues)
    End With
    ShowAll Ws1
End Sub
 
Upvote 0
oops forgot to include the other macro :oops::oops:
Goes in same module

Code:
Private Sub ShowAll(ws As Worksheet)
    On Error Resume Next
    ws.ShowAllData
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,583
Messages
6,114,487
Members
448,575
Latest member
hycrow

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