VBA Filter top 50 by criteria and loop

thedeadzeds

Active Member
Joined
Aug 16, 2011
Messages
451
Office Version
  1. 365
Platform
  1. Windows
Guys,


Is there a way to adapt the code below. It currently does the following but I would like to adapt to do as per the below steps. The final data will be a table not a range. Not sure if that makes a difference


Currently the code does the following ( code below)
filters by New call in field 10
Filters by the criteria in filed 13
Filters by person in field 16
Filters 50 records for that person
Copy and Paste to sheet 2


New adapted code would be as follows:

  • Filter by new call (or the value in cell A4 sheet1)
  • Filter by MOT, Service, Service & MOT
  • Filter by every person (field 16)
  • Select the first 50 records for that person ( can this 50 be based on a number in cell A3 sheet1 so I can chase the volume if needed)
  • Paste to sheet 2
  • Then repeat for the next person in field 16 and paste to the bottom of the existing data in sheet 2 and so on.
  • Also, Is there a way to over come if there is not 50 or the volume in cell A3 sheet1 then just copy what is there


Current code:

Code:
Sub Macro2()


    'Filter New Call
    Sheets("Sheet1").Select
    ActiveSheet.Range("$A$1:$P$1315").AutoFilter Field:=10, Criteria1:= _
        "New Call"
    'Filter the relevant Contcode
    ActiveSheet.Range("$A$1:$P$1315").AutoFilter Field:=13, Criteria1:=Array( _
        "MOT", "Service", "Service & MOT"), Operator:=xlFilterValues
    'Filter by person
    ActiveSheet.Range("$A$1:$P$1315").AutoFilter Field:=16, Criteria1:= _
        "Bethan"
    'Select top 50 and paste to Sheet 2
    Range("A1:A395").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
So I've managed to get this working to a certain degree by finding some code on here and adapting. Does anyone know how i change this part of the code "If i = 51" so i can point this at a value in cell A3 in sheet1?

Many thanks

Code:
Sub dural2()    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim i As Long, j As Long
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    
    'Filter New Call
    Sheets("Sheet1").Select
    ActiveSheet.Range("$A$1:$P$1315").AutoFilter Field:=5, Criteria1:= _
        "New Calls"
    'Filter the relevant Contcode
    ActiveSheet.Range("$A$1:$P$1315").AutoFilter Field:=14, Criteria1:=Array( _
        "MOT", "Service", "Service & MOT"), Operator:=xlFilterValues
    'Filter by person
    ActiveSheet.Range("$A$1:$P$1315").AutoFilter Field:=17, Criteria1:=Array( _
        "Bethan Mason", "Lorna Young"), Operator:=xlFilterValues
    
    i = 1
    For j = 1 To Rows.Count
        If sh1.Cells(j, 1).EntireRow.Hidden = False Then
            sh1.Cells(j, 1).EntireRow.Copy sh2.Cells(i, 1)
            i = i + 1
            If i = 51 Then Exit Sub
        End If
    Next j
End Sub
 
Upvote 0
Ok, so i've managed to refer the volume to be filter out to Sheet1 cell r1. All i need now is for this to run for every name in field 16. Can anyone help? I though just adding the names as array would work but that is not the case.

Code:
Sub dural23()    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim i As Long, j As Long
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    
    'Filter New Call
    Sheets("Sheet1").Select
    ActiveSheet.Range("$A$1:$P$5000").AutoFilter Field:=4, Criteria1:= _
        "New Calls"
    'Filter the relevant Contcode
    ActiveSheet.Range("$A$1:$P$5000").AutoFilter Field:=13, Criteria1:=Array( _
        "Polk MOT", "Polk Service", "Polk Service & MOT"), Operator:=xlFilterValues
    'Filter by person
    ActiveSheet.Range("$A$1:$P$5000").AutoFilter Field:=16, Criteria1:=Array( _
        "Bethan Mason"), Operator:=xlFilterValues
    
    i = 1
    For j = 1 To Rows.Count
        If sh1.Cells(j, 1).EntireRow.Hidden = False Then
            sh1.Cells(j, 1).EntireRow.Copy sh2.Cells(i, 1)
            i = i + 1
            If i = Range("Sheet1!R1").Value Then Exit Sub
        End If
    Next j
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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