VBA to filter top ten values then paste into the related tables

Bisky

New Member
Joined
Feb 22, 2018
Messages
1
Hi All,

Extremely new to the forum and VBA. I was wondering if anyone can help me out?

I am looking for a VBA Code that can run through a data dump and filter for the Top ten highest variations and paste the correct data into the related tables.

I also understand this could be done in Pivot Table however I am trying to increase my understanding of VBA.

If anyone could help me with my issue that would be most appreciated

Thanks in Advance ! :)
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Here is a generic subroutine that must be adapted for your situation:

Code:
Option Explicit

Sub FilterForTop10AndCopyResults()
    'Assumes that the data dump is on the Data worksheet
    '  and extracted data should be placed on Sheet2
    
    Dim lLastDataRow As Long
    Dim lLastSheet2ColERow As Long
    
    With Worksheets("Data")
        .AutoFilterMode = False
        lLastDataRow = .Cells(.Rows.Count, 1).End(xlUp).Row  'Last Populated row
    End With
    With Worksheets("Sheet2")
        .AutoFilterMode = False
        lLastSheet2ColERow = .Cells(.Rows.Count, 3).End(xlUp).Row
    End With

    'Criterion for Top 10 is in column H([COLOR="#FF0000"]8[/COLOR]) of the Data worksheet
    'If more complex criteria are required to define "highest variations"
    '  then apply appropriate filters to other columns or add a helper column
    '  that contains results of whatever calculations that are used to
    '  determine the "highest variations" and filter on that.
    Worksheets("Data").Range("$A$1").CurrentRegion.AutoFilter Field:=[COLOR="#FF0000"]8[/COLOR], _
        Criteria1:="10", Operator:=xlTop10Items
        
    'Assumes the data to be copied is in column C and should be copied to the
    '  [COLOR="#0000FF"]cell after last populated row [/COLOR]of column 5 on Sheet2
    Worksheets("Data").Range("C2:C" & lLastDataRow).SpecialCells(xlCellTypeVisible).Copy _
        Destination:=Worksheets("Sheet2").Cells(lLastSheet2ColERow [COLOR="#0000FF"]+ 1[/COLOR], 5)

    'Clear Filter from Data
    Worksheets("Data").AutoFilterMode = False
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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