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

Filter Data in VBA without an Excel Filter

March 4, 2015 Marcus Small

In Excel VBA it is possible to apply a filter directly to items contained within an array.  This method, little known, is a way of filtering without the more traditional auto filter.  I have not conducted any tests but as the filter is performed in memory I assume the process happens more quickly than a regular Excel auto filter.  This would make a nice Excel case study.

Here is a simple example of the Excel VBA method using a one dimensional array.

Option Explicit

Sub OneDFilter() 'Excel VBA 1D arry to filter.
Dim ar As Variant
Dim var As Variant

ar = Array("Banana", "Pear", "Apple", "Apple", "Banana", "Orange")
var = Filter(ar, "Apple", 0) 'Apple is the criteria
End Sub

The result is as follows:

Var = "Banana", "Pear", "Banana", "Orange"

The following VBA coding is a little more practical as it turns a 2 dimensional array (such as a cell reference) into a 1 dimensional array, then filters the 1D array. So the technique is based on a range, as I said it is far more practical.

Option Explicit

Sub FilterWithCode() 'Excel VBA to filter an array
Dim ar As Variant
Dim var As Variant
   ar = [A2:A15]
   ar = Application.Transpose(ar)
   var = Filter(ar, "Apple", 0) 'Apple is the criteria to remove
   Range("G1", Range("G" & UBound(var) + 1)).Value = Application.Transpose(var)
End Sub

The application.transpose line is repeated from the article on filtering multiple items with an Excel filter. That article Autofilter on Multiple Conditions is along the same lines as the filter inside an array.

More on this very useful technique can be found on SNB's website which has some insightful coding on a range of complex topics.

VBA for Smarties

Tags filter, data, index, array, VBA
← Showing Trends with a ChartExcel VBA to Sort an Array →

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.