Piotrek Szostak
Piotrek Szostak

Reputation: 1

VBA Copy visible cells and paste as values - most efficient method

I have an excel tool that uses feed from 11 different raw data files generated from reporting system to do some calculations on that data.

The process is very simple: - open file - filter contents - copy filtered contents - paste into another file (the summary tool's tab)

As the amounts of data and individual files rose I started getting more issues with memory.Hence my question - what is the most memory and speed efficient way to copy/paste these tables?

Maybe there is yet another method that I don't know of?

Appreciate any insights!

Upvotes: 0

Views: 3010

Answers (1)

Gary's Student
Gary's Student

Reputation: 96771

Fortunately there is a built-in feature for Autofilter to facilitate this. Say we start with:

enter image description here

and apply a filter to it with:

Sub Macro1()
    Columns("A:B").AutoFilter
    ActiveSheet.Range("$A$1:$B$8").AutoFilter Field:=2, Criteria1:=">50", Operator:=xlAnd
End Sub

enter image description here

==> The Autofilter has a Range property that allows: <==

Sub Kopy()
    Dim rng As Range
    Set rng = ActiveSheet.AutoFilter.Range
    rng.Copy Sheets("Sheet2").Range("A1")
End Sub

The result on Sheet2 has

  1. only the visible data from Sheet1
  2. the header row
  3. is not filtered

enter image description here

NOTE:

Looping is not required.
Similar approach can be used for Tables.
SpecialCells is not required.

Upvotes: 1

Related Questions