Excel

TheSmallman.com

an XL ideas Lab

Dashboards VBA
  • Home
  • Dashboards
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
    • Power Pivot a User Guide
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About
Menu

Excel Dashboards VBA

Street Address
City, State, Zip
Phone Number
an XL ideas Lab

Your Custom Text Here

Excel Dashboards VBA

  • Home
  • Dashboards
  • Excel Tips
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
  • PowerPivot
    • Power Pivot a User Guide
  • Courses
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About

Copy/Paste Top n Visible Rows

November 27, 2016 Marcus Small
Photo by Patrick Brinksma on Unsplash

Photo by Patrick Brinksma on Unsplash


I recently had a question regarding copying data after a filter had been applied. What if we wanted the top n items from a list to be copied to another place. The problem to address here is that a filtered list can have cells which are hundreds or thousands of rows apart. So copying sequentially might not work. Also, you may not want to Filter by top n. So selecting the top n visible cells in a filtered list becomes a bit problematic. I suppose the simple solution is to copy that filtered list to a new location - only the filtered items will appear and they will be sequential. The following procedure counts the rows from 1 to 10 in a filtered list and copies the results and places them on Sheet 2 in cells A2.

Sub TopNRows()
Dim i As Long
Dim r As Range
Dim rWC As Range

Set r = Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(12)

For Each rwC In r
    i = i + 1
    If i = 10 Or i = r.Count Then Exit For
Next rwC
Range(r(1), rwC).Resize(, 12).SpecialCells(12).Copy sheet2.[A2]
End Sub

The procedure runs on the active sheet and will paste data to the worksheet code name (sheet2). Be sure to check that you actually have a sheet 2 in the VBA editor if you are going to use this procedure. The attached file should help crystalise the procedure. Also the procedure is for the first 12 columns (resize, 12) is to create a larger area to copy.

Enjoy

TopnRecords.xls

← Compare Two Lists on Different Sheets and Output Matches Dependent and Non Dependent Comboboxes →

Featured Posts

Excel Dashboards: Tracking a Crisis

Excel Dashboards: Tracking a Crisis
April 14, 2020

Recent Posts

Populating an Excel Table from a Range of Cells with VBA

Populating an Excel Table from a Range of Cells with VBA June 12, 2025

Fuzzy Distribution with Randbetween

Fuzzy Distribution with Randbetween May 21, 2025

Add Minimum and Maximum for Chart in Cells

Add Minimum and Maximum for Chart in Cells March 12, 2025

Inflation Over Multiple Years in a Single Cell

Inflation Over Multiple Years in a Single Cell January 10, 2025

Hubspot Dashboard

Hubspot Dashboard October 3, 2024

Monthly Dashboard With Supporting Metrics

Monthly Dashboard With Supporting Metrics September 25, 2024

Excel Show Missing Sheet Tabs

Excel Show Missing Sheet Tabs July 29, 2024

Run Macro Overnight Automatically

Run Macro Overnight Automatically June 24, 2024

Split File into Parts and Save to Directory

Split File into Parts and Save to Directory April 20, 2024

Most Popular Author

Most Popular Author December 14, 2023

 

Follow US:

 
 

MarcusSmall@thesmallman.com

 

TheSmallman.com - Making your small systems hum...
© Copyright 2013-2024 theSmallman.com All Rights Reserved.