Macro to filter and copy first 10 lines of data only

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
2,575
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
 

Some videos you may like

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,578
Office Version
  1. 365
Platform
  1. Windows
Easiest way would be to copy the filtered data & then delete row 11 onwards.
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,781
Office Version
  1. 365
Platform
  1. Windows
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
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,781
Office Version
  1. 365
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,446
Messages
5,528,802
Members
409,836
Latest member
karnasrinivas
Top