Copy filtered data to another sheet using VBA

123,448

Best way of doing it

Below code is to copy the visible data in DBExtract sheet, and paste it into duplicateRecords sheet, with only filtered values. Range selected by me is the maximum range that can be occupied by my data. You can change it as per your need.

  Sub selectVisibleRange()

    Dim DbExtract, DuplicateRecords As Worksheet
    Set DbExtract = ThisWorkbook.Sheets("Export Worksheet")
    Set DuplicateRecords = ThisWorkbook.Sheets("DuplicateRecords")

    DbExtract.Range("A1:BF9999").SpecialCells(xlCellTypeVisible).Copy
    DuplicateRecords.Cells(1, 1).PasteSpecial


    End Sub
Share:
123,448
Ananya Pandey
Author by

Ananya Pandey

I have experience in Data center, under four domain ( Network, Security, Servers and Storage) from TCS. I am currently pursuing my MBA in General Management from CUIM, Bangalore. I seek my career in business intelligence, currently learning programming, R, Python, Analytics, Data Product development etc.

Updated on October 07, 2020

Comments

  • Ananya Pandey
    Ananya Pandey over 3 years

    I have two sheets. One has the complete data and the other is based on the filter applied on the first sheet.

    Name of the data sheet : Data
    Name of the filtered Sheet : Hoky

    I am just taking a small portion of data for simplicity. MY objective is to copy the data from Data Sheet, based on the filter. I have a macro which somehow works but its hard-coded and is a recorded macro.

    My problems are:

    1. The number of rows is different everytime. (manual effort)
    2. Columns are not in order.

    enter image description here enter image description here

    Sub TESTTHIS()
    '
    ' TESTTHIS Macro
    '
    'FILTER
    Range("F2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$B$2:$F$12").AutoFilter Field:=5, Criteria1:="hockey"
    
    'Data Selection and Copy
    Range("C3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Hockey").Select
    Range("E3").Select
    ActiveSheet.Paste
    
    Sheets("Data").Select
    Range("D3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Hockey").Select
    Range("D3").Select
    ActiveSheet.Paste
    
    Sheets("Data").Select
    Range("E3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Hockey").Select
    Range("C3").Select
    ActiveSheet.Paste
    
    End Sub
    
  • Ananya Pandey
    Ananya Pandey over 7 years
    Can you write an example (complete lne of code)so that i can apply it on my sheet.
  • Ananya Pandey
    Ananya Pandey over 7 years
    It worked fine. Thanks. and i got the idea although i have to know more about the offset function.
  • Arpan Saini
    Arpan Saini over 6 years
    This is a very time consuming process and take lot of time to read each row and copy it to another sheet, Sheet will hang when you have data in thousands record
  • ashleedawg
    ashleedawg about 6 years
    @AnanyaPandey This isn't a "free code writing service". Please see How to Ask and help center.
  • Ananya Pandey
    Ananya Pandey almost 6 years
    With all due respect Mister, I am not asking for free service, I tried i failed and seeking help here i am learning, Thank you for your valuable comment.
  • Florian Gee
    Florian Gee over 3 years
    SpecialCells(xlCellTypeVisible) was exactly what I was looking for. Thanks!