<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:media="http://search.yahoo.com/mrss/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>The VBA Tutorials Blog</title>
    <description>Each VBA Tutorial contains macro examples to help you learn VBA code quickly. Follow these VBA tutorials to learn basic and advanced Excel macro programming.</description>
    <link>https://wellsr.com/vba/</link>
    <atom:link href="https://wellsr.com/vba/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 14 Dec 2025 18:36:53 -0500</pubDate>
    <lastBuildDate>Sun, 14 Dec 2025 18:36:53 -0500</lastBuildDate>
    <generator>Jekyll v2.5.3</generator>
    
      <item>
        <title>How to Autofill Using Excel VBA</title>
        <description>&lt;p&gt;In this tutorial, you will learn how to use the Autofill feature in Excel VBA to fill a range of cells with data based on a pattern or a series. Autofill is a useful tool that can save you time and avoid errors when you need to enter repetitive or sequential data in your worksheets.&lt;/p&gt;

&lt;h2 id=&quot;what-is-autofill&quot;&gt;What is Autofill?&lt;/h2&gt;

&lt;p&gt;Autofill is a feature in Excel that allows you to automatically fill a range of cells with data based on the first cell or cells in the range. For example, if you enter 1 in cell A1 and 2 in cell A2, select both cells then drag the fill handle (the small square at the bottom right corner of the cell) to cell A10, Excel will autofill the range A1:A10 with the numbers 1 to 10. Similarly, if you enter Jan in cell B1 and drag the fill handle to cell B12, Excel will autofill the range B1:B12 with the months of the year.&lt;/p&gt;

&lt;p&gt;You can also use Autofill to fill a range of cells with custom lists that you create or modify in Excel. For example, you can create a list of colors and use Autofill to fill a range of cells with those colors.&lt;/p&gt;

&lt;h2 id=&quot;how-to-use-autofill-in-excel-vba&quot;&gt;How to Use Autofill in Excel VBA&lt;/h2&gt;

&lt;p&gt;You can use the Range.Autofill method in Excel VBA to perform the same action as dragging the fill handle in Excel. The syntax of the Range.Autofill method is:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFill&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Destination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlFillDefault&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;Range&lt;/code&gt; argument specifies the range of cells that contains the data you want to use as the basis for autofilling. The &lt;code&gt;Destination&lt;/code&gt; argument specifies the range of cells you want to fill with data. The &lt;code&gt;Type&lt;/code&gt; argument specifies the type of autofilling you want to apply. The possible values for this argument are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;xlFillDefault&lt;/strong&gt;: Fills the destination range with data based on the default behavior of Excel. The default can fill the destination range with formulas, numbers, or text based on the source cell.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlFillCopy&lt;/strong&gt;: Copies the data from the source range to the destination range without any changes.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlFillSeries&lt;/strong&gt;: Fills the destination range with a series of values based on the source range.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlFillFormats&lt;/strong&gt;: Copies only the formats from the source range to the destination range.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlFillValues&lt;/strong&gt;: Copies only the values from the source range to the destination range.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlFillDays&lt;/strong&gt;: Fills the destination range with a series of dates incremented by one day.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlFillWeekdays&lt;/strong&gt;: Fills the destination range with a series of dates incremented by one weekday.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlFillMonths&lt;/strong&gt;: Fills the destination range with a series of dates incremented by one month.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlFillYears&lt;/strong&gt;: Fills the destination range with a series of dates incremented by one year.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlGrowthTrend&lt;/strong&gt;: Fills the destination range with a growth trend or a geometric series of numbers based on the source range.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlLinearTrend&lt;/strong&gt;: Fills the destination range with a linear trend based on the source range.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;xlFlashFill&lt;/strong&gt;: Fills the destination range with data based on Flash Fill, which is an intelligent feature that analyzes your data and fills in missing values or extracts values from other columns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that not all types of autofilling are applicable to all types of data. For example, you cannot use &lt;code&gt;xlFillDays&lt;/code&gt; to fill a range of cells with text values.&lt;/p&gt;

&lt;h3 id=&quot;example-1-how-to-autofill-using-excel-vba&quot;&gt;Example 1: How to Autofill Using Excel VBA&lt;/h3&gt;

&lt;p&gt;Let’s see an example of how to use Autofill in Excel VBA. Suppose you have a worksheet named “Sales” containing the following data:&lt;/p&gt;

&lt;table class=&quot;table table-striped table-bordered&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;A&lt;/th&gt;
            &lt;th&gt;B&lt;/th&gt;
            &lt;th&gt;C&lt;/th&gt;
            &lt;th&gt;D&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;Jan&lt;/td&gt;
            &lt;td&gt;1000&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Feb&lt;/td&gt;
            &lt;td&gt;1200&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Mar&lt;/td&gt;
            &lt;td&gt;1400&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;We want to use Autofill to fill column A with names of the months and to fill colum B with a linear trend based on the existing values column B. To do this, we can use the following VBA code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AutofillExample&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;&#39;Activate worksheet&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Worksheets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Sales&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Activate&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;&#39;Select source range&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startingRange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ActiveSheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A1:A2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;&#39;Autofill destination range with xlFillMonths&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;startingRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFill&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Destination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A1:A12&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlFillMonths&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;&#39;Select source range&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startingRange&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ActiveSheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B1:B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;&#39;Autofill destination range with xlLinearTrend&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;startingRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFill&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Destination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B1:B12&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlLinearTrend&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When you run this code, columns A and B will be filled, like this:&lt;/p&gt;

&lt;table class=&quot;table table-striped table-bordered&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;A&lt;/th&gt;
            &lt;th&gt;B&lt;/th&gt;
            &lt;th&gt;C&lt;/th&gt;
            &lt;th&gt;D&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;Jan&lt;/td&gt;
            &lt;td&gt;1000&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Feb&lt;/td&gt;
            &lt;td&gt;1200&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Mar&lt;/td&gt;
            &lt;td&gt;1400&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Apr&lt;/td&gt;
            &lt;td&gt;1600&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;May&lt;/td&gt;
            &lt;td&gt;1800&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Jun&lt;/td&gt;
            &lt;td&gt;2000&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Jul&lt;/td&gt;
            &lt;td&gt;2200&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Aug&lt;/td&gt;
            &lt;td&gt;2400&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Sep&lt;/td&gt;
            &lt;td&gt;2600&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Oct&lt;/td&gt;
            &lt;td&gt;2800&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Nov&lt;/td&gt;
            &lt;td&gt;3000&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;Dec&lt;/td&gt;
            &lt;td&gt;3200&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;example-2-fill-a-range-with-the-same-value&quot;&gt;Example 2: Fill a range with the same value&lt;/h3&gt;

&lt;p&gt;Suppose you have a worksheet like this:&lt;/p&gt;

&lt;table class=&quot;table table-striped table-bordered&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;A&lt;/th&gt;
            &lt;th&gt;B&lt;/th&gt;
            &lt;th&gt;C&lt;/th&gt;
            &lt;th&gt;D&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Hello&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;4&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;And you want to fill column B with the value “Hello”. You can use VBA Autofill to do this in one line of code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AutofillExample2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFill&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Destination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B1:B4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlFillDefault&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code will copy the value in B1 and fill the rest of the range B1:B4 with the same value. The result will look like this:&lt;/p&gt;

&lt;table class=&quot;table table-striped table-bordered&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;A&lt;/th&gt;
            &lt;th&gt;B&lt;/th&gt;
            &lt;th&gt;C&lt;/th&gt;
            &lt;th&gt;D&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;Hello&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;2&lt;/td&gt;
            &lt;td&gt;Hello&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;3&lt;/td&gt;
            &lt;td&gt;Hello&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;4&lt;/td&gt;
            &lt;td&gt;Hello&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Again, the Type argument specifies how to fill the range. In this case, we use xlFillDefault, which means to copy the value or formula in the first cell. You can also use other types, such as xlFillCopy, xlFillSeries, xlFillValues, etc.&lt;/p&gt;

&lt;h3 id=&quot;example-3-fill-a-range-with-a-series-of-values&quot;&gt;Example 3: Fill a range with a series of values&lt;/h3&gt;

&lt;p&gt;Suppose you have a worksheet like this:&lt;/p&gt;

&lt;table class=&quot;table table-striped table-bordered&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;A&lt;/th&gt;
            &lt;th&gt;B&lt;/th&gt;
            &lt;th&gt;C&lt;/th&gt;
            &lt;th&gt;D&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;10&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;20&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;30&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;40&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;And you want to fill column B with a series of values that starts from a number that’s 5 greater than the value in Cell A1. You can use VBA Autofill to do this in two lines of code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AutofillExample3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFill&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Destination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B1:B4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlFillSeries&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code will first assign the value in A1 plus 5 to B1, and then fill the rest of the range B1:B4 with a series of values that increases by 1. The result will look like this:&lt;/p&gt;

&lt;table class=&quot;table table-striped table-bordered&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;A&lt;/th&gt;
            &lt;th&gt;B&lt;/th&gt;
            &lt;th&gt;C&lt;/th&gt;
            &lt;th&gt;D&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;10&lt;/td&gt;
            &lt;td&gt;15&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;20&lt;/td&gt;
            &lt;td&gt;16&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;30&lt;/td&gt;
            &lt;td&gt;17&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;40&lt;/td&gt;
            &lt;td&gt;18&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;example-4-fill-a-range-with-formulas&quot;&gt;Example 4: Fill a range with formulas&lt;/h3&gt;

&lt;p&gt;Suppose you have a worksheet like this:&lt;/p&gt;

&lt;table class=&quot;table table-striped table-bordered&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;A&lt;/th&gt;
            &lt;th&gt;B&lt;/th&gt;
            &lt;th&gt;C&lt;/th&gt;
            &lt;th&gt;D&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;x&lt;/td&gt;
            &lt;td&gt;f(x)&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;-2&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;-1&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;0&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;1&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;And you want to fill column B with formulas that calculate &lt;kbd&gt;f(x) = -0.0625x^2 + x + 0.5&lt;/kbd&gt;. You can use VBA Autofill to do this in two lines of code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AutofillExample4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Formula&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=-0.0625*A2^2+A2+0.5&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFill&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Destination&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2:B5&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlFillCopy&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This code will first assign the formula to B2, and then fill the rest of the range B2:B5 with the same formula. The result will look like this:&lt;/p&gt;

&lt;table class=&quot;table table-striped table-bordered&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;A&lt;/th&gt;
            &lt;th&gt;B&lt;/th&gt;
            &lt;th&gt;C&lt;/th&gt;
            &lt;th&gt;D&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;x&lt;/td&gt;
            &lt;td&gt;f(x)&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;-2&lt;/td&gt;
            &lt;td&gt;-1.75&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;-1&lt;/td&gt;
            &lt;td&gt;-0.5625&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;-0&lt;/td&gt;
            &lt;td&gt;0.5&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;-1&lt;/td&gt;
            &lt;td&gt;1.4375&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;            
        &lt;/tr&gt;        
    &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;In this example, you would have gotten the same result if you used the xlFillSeries or the xlFillDefault Type. That just goes to show you that there are often multiple Type parameters that can be used to accomplish the same VBA AutoFill behavior.&lt;/p&gt;

&lt;p&gt;I hope this tutorial was helpful and you learned how to use AutoFill in VBA to fill a range with different types of series. Thank you for reading!&lt;/p&gt;
</description>
        <pubDate>Wed, 12 Apr 2023 14:15:00 +0000</pubDate>
        <media:content url="https://wellsr.com/vba/assets/images/2023-04-12-vba-autofill-tutorial-with-examples.png" medium="image"></media:content>
        <link>https://wellsr.com/vba/2023/excel/how-to-autofill-using-excel-vba/</link>
        <guid isPermaLink="true">https://wellsr.com/vba/2023/excel/how-to-autofill-using-excel-vba/</guid>
        
        
        <category>excel</category>
        
      </item>
    
      <item>
        <title>How to use the VBA NPV Function</title>
        <description>&lt;p&gt;In this tutorial, you will learn how to use the VBA &lt;strong&gt;NPV&lt;/strong&gt; function to calculate the net present value of an investment based on a series of cash flows and a discount rate. The net present value is a measure of how much an investment is worth today, considering the time value of money.&lt;/p&gt;

&lt;p&gt;The VBA NPV function has the following syntax:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;NPV&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ValueArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Rate is the discount rate per period, expressed as a decimal. For example, if the annual discount rate is 10%, and the cash flows are monthly, then the rate per period is 0.1 / 12 = 0.008333.&lt;/li&gt;
  &lt;li&gt;ValueArray is an array of cash flow values, where negative values represent payments and positive values represent receipts. The NPV function assumes that the first cash flow occurs at the end of the first period.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;using-the-npv-function-with-an-array&quot;&gt;Using the NPV Function with an Array&lt;/h2&gt;

&lt;p&gt;To use the VBA NPV function, you need to declare a variable to store the result, and assign it the value of the function with the appropriate arguments. For example, suppose you have an investment that requires an initial payment of $10,000, and then generates monthly cash flows of $1,000 for 12 months. The annual discount rate is 10%. You can calculate the net present value of this investment as follows:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NPV_Demo1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;NetPV&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Declare a variable to store the result&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;NetPV&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NPV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Assign it the value of the NPV function&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;MsgBox&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;The net present value of this investment is &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NetPV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Currency&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Display the result&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The result is &lt;strong&gt;$1,363.15&lt;/strong&gt;, which means that this investment is worth more than its cost today.&lt;/p&gt;

&lt;p&gt;Note that if your first cash flow occurs at the beginning of the first period, you need to adjust your calculation by adding the first value to the result of the NPV function. For example, suppose you have an investment that requires an initial payment of $10,000 at the beginning of the first month, and then generates monthly cash flows of $1,000 for 12 months. The annual discount rate is 10%. You can calculate the net present value of this investment as follows:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NPV_Demo2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;NetPV&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Declare a variable to store the result&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;NetPV&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NPV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Add the first value to the result of the NPV function&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;MsgBox&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;The net present value of this investment is &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NetPV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Currency&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Display the result&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The result is &lt;strong&gt;$1,374.51&lt;/strong&gt;, which is higher than the previous case because you pay less interest on your initial payment.&lt;/p&gt;

&lt;h2 id=&quot;using-the-npv-function-with-a-loop&quot;&gt;Using the NPV Function with a Loop&lt;/h2&gt;

&lt;p&gt;You can also use a loop to populate your ValueArray with dynamic values from a worksheet range or a user input. For example, suppose you have a worksheet with the following data:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;A&lt;/th&gt;
      &lt;th&gt;B&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Discount Rate&lt;/td&gt;
      &lt;td&gt;10%&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Initial Payment&lt;/td&gt;
      &lt;td&gt;-10,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Cash Flow&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 1&lt;/td&gt;
      &lt;td&gt;1,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 2&lt;/td&gt;
      &lt;td&gt;1,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 3&lt;/td&gt;
      &lt;td&gt;1,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 4&lt;/td&gt;
      &lt;td&gt;1,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 5&lt;/td&gt;
      &lt;td&gt;1,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 6&lt;/td&gt;
      &lt;td&gt;-2,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 7&lt;/td&gt;
      &lt;td&gt;-2,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 8&lt;/td&gt;
      &lt;td&gt;-2,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 9&lt;/td&gt;
      &lt;td&gt;-2,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 10&lt;/td&gt;
      &lt;td&gt;-2,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 11&lt;/td&gt;
      &lt;td&gt;-2,000&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Month 12&lt;/td&gt;
      &lt;td&gt;-2,000&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;You can calculate the net present value of this investment as follows:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;NPV_Demo3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;NetPV&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Declare a variable to store the result&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Rate&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Declare a variable to store the discount rate&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ValueArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Double&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Declare an array to store the cash flow values&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Long&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Declare a variable to loop through rows&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Long&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Declare a variable to count rows&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Rate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;12&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Get the discount rate per month&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WorksheetFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B4:B15&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Count the number of cash flow values&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ReDim&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ValueArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;To&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Resize the array to match the number of values&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;For&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;To&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Loop through each row&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ValueArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Assign the value to the array element&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Go to the next row&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;NetPV&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WorksheetFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NPV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Rate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ValueArray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Calculate the net present value using the NPV function&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;NetPV&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NetPV&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Add the initial payment to the result&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;MsgBox&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;The net present value of this investment is &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NetPV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Currency&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;Display the result in a message box&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this example, we used a loop to populate the ValueArray with values from a worksheet range. This way, we can easily change the values in the worksheet and recalculate the net present value without modifying the code. We also used the &lt;code&gt;WorksheetFunction&lt;/code&gt; object to access the Excel NPV function from VBA instead of the native VBA worksheet function. The &lt;code&gt;WorksheetFunction&lt;/code&gt; object allows us to use any of the built-in Excel functions in our code, but we could have easily just used the native VBA NPV function, like we did earlier, and we would have gotten the same result: &lt;strong&gt;($-18,116.84)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Using a loop and an array is a common technique in VBA programming. It can help us simplify our code and make it more dynamic and flexible. There are certainly more efficient ways to extract data from an Excel spreadsheet, but I wanted to demonstrate with loops and arrays since they can be adapted to many use cases.&lt;/p&gt;

&lt;p&gt;In this tutorial, we learned how to use the VBA &lt;strong&gt;NPV&lt;/strong&gt; function to calculate the net present value of an investment based on a series of cash flows and a discount rate. We also demonstrated how to use a loop and an array to calculate the net present value of an investment using VBA, both by using the native VBA NPV function and by using the &lt;code&gt;WorksheetFunction&lt;/code&gt; object to access Excel functions from VBA. I hope you found this tutorial useful and informative. For more VBA tips and tricks like this, please subscribe using the form below. Thank you for reading!&lt;/p&gt;

</description>
        <pubDate>Mon, 03 Apr 2023 18:30:00 +0000</pubDate>
        <media:content url="https://wellsr.com/vba/assets/images/2023-04-02-using-the-vba-npv-function.png" medium="image"></media:content>
        <link>https://wellsr.com/vba/2023/excel/vba-npv-function/</link>
        <guid isPermaLink="true">https://wellsr.com/vba/2023/excel/vba-npv-function/</guid>
        
        <category>math</category>
        
        
        <category>excel</category>
        
      </item>
    
      <item>
        <title>FormulaR1C1 Makes Relative References in VBA Easy</title>
        <description>&lt;p&gt;A1-style notation for spreadsheets is intuitive and easy to work with. Columns are always identified by letters and rows always by numbers, so it’s hard to confuse which is which. The prevalence of using A1-notation is so thorough that sometimes it’s hard to imagine any other way to do it. But another way does exist.&lt;/p&gt;

&lt;p&gt;In some circumstances, it makes more sense to use R1C1-style notation, which identifies the entire Excel grid by number. This becomes particularly useful when you need to perform some arithmetic on the column and row placement, such as moving down two rows and left one column.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#pastexperience&quot;&gt;Connecting R1C1 to Past Experiences &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#powerofr1c1&quot;&gt;The Power of R1C1 &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#typesofrefs&quot;&gt;Types of R1C1 Reference &lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#absoluterefs&quot;&gt;Absolute References &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#relativerefs&quot;&gt;Relative References &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#mixedrefs&quot;&gt;Mixed References &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#locking&quot;&gt;Lock Rows and Lock Columns with R1C1 &lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#variablerefs&quot;&gt;Variable R1C1 References &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#examplemodel&quot;&gt;Modeling Variable Client Closing Times &lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#thesetup&quot;&gt;The Setup &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#thecode&quot;&gt;The Code &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#theresult&quot;&gt;The Result &lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;pastexperience&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Connecting R1C1 to Past Experiences&lt;/h2&gt;
&lt;p&gt;R1C1 is just as intuitive as A1 notation once you’re accustomed to it. Many programmers already have plenty of experience with referencing ranges with R1C1 notation, because this is how the &lt;code&gt;.Cells&lt;/code&gt; object works in Excel. It is very common to loop through a set of cells like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;k&quot;&gt;For&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;To&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;For&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;To&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;Cells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The code block above moves through a 10-by-5 grid and multiplies each number by 1000. References like this identify the row and the column by number, which is exactly what R1C1 does, albeit in a slightly different manner.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;powerofr1c1&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Power of R1C1&lt;/h2&gt;
&lt;p&gt;R1C1 notation take the same concept above and applies it to formulas. Instead of entering into cell B4 the formula &lt;code&gt;=B3*1.05&lt;/code&gt; to increase B3 by 5%, we could write into B4 (R2C4) &lt;code&gt;=RC[-1]*1.05&lt;/code&gt;, which references&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;the row offset by 0 (nothing)&lt;/li&gt;
  &lt;li&gt;the column offset by -1 (one to the left).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A consequence of this is that when dragging or autofilling, in A1 notation we’ll have formulas that look different in each cell but in R1C1 notation the formulas will all be exactly the same:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2022-01-21-vba-formular1c1-formula-a1-style.PNG&quot; alt=&quot;Simple A1-Style Notation Formulas&quot; title=&quot;Simple A1-Style Notation Formulas&quot; /&gt;&lt;br /&gt;&lt;b&gt;Simple A1-Style Notation Formulas&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2022-01-21-vba-formular1c1-formula-r1c1-style.PNG&quot; alt=&quot;Simple R1C1-Style Notation Formulas&quot; title=&quot;Simple R1C1-Style Notation Formulas&quot; /&gt;&lt;br /&gt;&lt;b&gt;Simple R1C1-Style Notation Formulas&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;This visualization should be intuitive evidence of how powerful R1C1 notation can be: a single formula based on referential logic can generate complex tables. There is also a lot of flexibility in this referential style, which we’ll explore further.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;typesofrefs&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Types of R1C1 Reference&lt;/h2&gt;
&lt;p&gt;A1-style notation has a single type. You enter the column letter and the row number and that’s it. There are no other ways to make the reference. With R1C1, however, you can make &lt;em&gt;absolute&lt;/em&gt; references, &lt;em&gt;relative&lt;/em&gt; references, and &lt;em&gt;mixed&lt;/em&gt; references.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;absoluterefs&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Absolute References&lt;/h3&gt;
&lt;p&gt;Absolute references in R1C1-style target a specific cell and they never change. For example, let’s add some absolute references to B2 and C2:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R2C1 * 1.05&quot;&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;&#39;set by A1 range notation&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Cells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R3C1 * 1.05&quot;&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;&#39;set by row/column cell notation&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These two formulae reference R2C1 (cell A2) and R3C1 (cell A3) at all times, and they never change, even if you drag them across the sheet. It’s equivalent to typing &lt;code&gt;=$A$2 * 1.05&lt;/code&gt; and &lt;code&gt;=$A$3 * 1.05&lt;/code&gt; directly in the Excel cell.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;relativerefs&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Relative References&lt;/h3&gt;
&lt;p&gt;Relative references are a little more interesting, because they let you leverage spatial logic. Simply use square brackets &lt;code&gt;[]&lt;/code&gt; to make the R1C1 notation relative rather than absolute, just as we did earlier in the Power of R1C1 section.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Positive numbers reference cells down ↓ or to the right →&lt;/li&gt;
  &lt;li&gt;Negative numbers reference cells above ↑ or to the left ←&lt;/li&gt;
  &lt;li&gt;No number (nothing ~=0) maintains the current row or column&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The positive/negative difference is intuitive, at least for those of us who read top-to-bottom and left-to-right. A couple examples may help:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R[-1]C[-1]&quot;&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;&#39;refs A1, one row up (-1) and one column left (-1)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R[2]C[-1]&quot;&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;&#39;refs A4, two rows down (+2) and one column left (-1)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=RC[3]&quot;&lt;/span&gt;              &lt;span class=&quot;c1&quot;&gt;&#39;refs E2, zero row changes and three columns right (+3)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R[0]C[3]&quot;&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;&#39;refs E2, zero row changes and three columns right (+3)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice how similar this syntax is to the &lt;a href=&quot;/vba/2016/excel/offset-vba-property-to-navigate-excel/&quot;&gt;VBA Offset&lt;/a&gt; property. With these relative references, if you drag or autofill the formulas on the worksheet, you’ll get a the same spatial relations. The first formula above in B2 references A1, but if we put it into C3, it would reference B2. This contrasts with the absolute reference, which will &lt;em&gt;always&lt;/em&gt; reference the same cell, no matter where it is on the sheet.&lt;/p&gt;

&lt;hr /&gt;

&lt;p style=&quot;margin-bottom:10px;&quot;&gt;&lt;span style=&quot;font-size:26px;color:#000000;&quot;&gt;Make powerful macros with our free VBA Developer Kit&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;padding-top:10px;&quot;&gt;&lt;p&gt;It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free &lt;b&gt;VBA Developer Kit&lt;/b&gt; and wrote the &lt;b&gt;Big Book of Excel VBA Macros&lt;/b&gt; full of hundreds of pre-built macros to help you master file I/O, arrays, strings and more - grab your free copy below.&lt;/p&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;centered&quot;&gt;
&lt;form action=&quot;//wellsr.us13.list-manage.com/subscribe/post?u=9fa014ad47345eb4b70403d38&amp;amp;id=5c804a55ec&quot; method=&quot;post&quot; class=&quot;form-inline validate&quot; target=&quot;_blank&quot; id=&quot;email-subscribe&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-md-10 col-md-offset-1&quot;&gt;&lt;input required=&quot;&quot; type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;consent&quot; id=&quot;mce-Checkbox&quot; /&gt; I&#39;ll take a free VBA Developer Kit&lt;br /&gt;&lt;/div&gt;
&lt;input type=&quot;email&quot; value=&quot;&quot; name=&quot;EMAIL&quot; class=&quot;form-control email&quot; id=&quot;mce-EMAIL&quot; placeholder=&quot;Enter your email address&quot; required=&quot;&quot; /&gt;
&lt;div class=&quot;mc-field-group input-group&quot; style=&quot;display:none&quot;&gt;
    &lt;strong&gt;Programming Language &lt;/strong&gt;
    &lt;ul&gt;&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;1&quot; name=&quot;group[17657][1]&quot; id=&quot;mce-group[17657]-17657-0&quot; checked=&quot;&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-0&quot;&gt;VBA&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;group[17657][2]&quot; id=&quot;mce-group[17657]-17657-1&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-1&quot;&gt;Python&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups--&gt;
&lt;div style=&quot;position: absolute; left: -5000px;&quot; aria-hidden=&quot;true&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;b_9fa014ad47345eb4b70403d38_5c804a55ec&quot; tabindex=&quot;-1&quot; value=&quot;&quot; /&gt;&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-success btn-lg&quot; name=&quot;subscribe&quot; id=&quot;mc-embedded-subscribe&quot;&gt;Get my free kit&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;!--End mc_embed_signup--&gt;
&lt;/div&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;mixedrefs&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Mixed References &lt;/h3&gt;
&lt;p&gt;These two styles can be combined, too. Simply add or exclude the square brackets to mix.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R1C[-1]&quot;&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;&#39;refs A1, but the row will always be 1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R[2]C2&quot;&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;&#39;refs B4, but the column will never change (always B)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;locking&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Lock Rows and Lock Columns with R1C1&lt;/h3&gt;
&lt;p&gt;You may have noticed that for absolute references, the column or row never changes. This seems awfully similar to locking a row or column - and that is exactly what it does.&lt;/p&gt;

&lt;p&gt;An absolute reference, such as &lt;code&gt;=R1C2 * 1.05&lt;/code&gt; will &lt;em&gt;always&lt;/em&gt; grow cell B1 by 5%, no matter where it is entered in the worksheet. Again, in the more familiar A1 notation, this formula would appear as &lt;code&gt;=$B$2 * 1.05&lt;/code&gt;. These two formulas are identical, except one is in R1C1 notation and the other is in A1 notation. Let’s take a look at a couple more examples with locked rows and columns.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R1C[-1]*1.05&quot;&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;&#39;equivalent to A$1 * 1.05, as the row is locked&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R[2]C2*1.05&quot;&lt;/span&gt;             &lt;span class=&quot;c1&quot;&gt;&#39;equivalent to $B4 * 1.05, as the column is locked&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that when making absolute references, it doesn’t matter what your starting cell is. The absolute reference must be specified.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;variablerefs&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Variable R1C1 References &lt;/h2&gt;
&lt;p&gt;Variables are a core component of programming. There isn’t much point to programming if some value isn’t varying.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Range.FormulaR1C1&lt;/code&gt; inputs a formula into a cell, and the formula is written as a string in the VBA code. This means you can &lt;a href=&quot;/vba/2018/excel/vba-concatenate-strings-with-ampersand/&quot;&gt;concatenate&lt;/a&gt; any numerical variable into the R1C1 formula using string concatenation.&lt;/p&gt;

&lt;p&gt;This comes in handy when coupled with relative references, allowing you to target different cell distances based on variables.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;numMonths&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R[-1]C[&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numMonths&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;]*1.05&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here, we place a formula in cell B2 that increases a value by 5%. The cell we’re referencing is in Row 1 (from &lt;code&gt;R[-1]&lt;/code&gt;) and some column determined by the value &lt;mod&gt;numMonths&lt;/mod&gt; in cell A1. If the number of months is 6, then the formula in B2 would be &lt;code&gt;=R[-1]C[6] * 1.05&lt;/code&gt; or, based on its initial cell B2, &lt;code&gt;=H1 * 1.05&lt;/code&gt;, which is one row above B2 and 6 columns to the right.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;examplemodel&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Modeling Variable Client Closing Times &lt;/h2&gt;
&lt;p&gt;I always like to give examples, and I recently had the opportunity to apply this type of variable R1C1 referencing at my full-time job (which is not actually programming).&lt;/p&gt;

&lt;p&gt;I was building a financial model that included multiple client types and each type had a different length of time from lead-in to contract signing. The time to close greatly affects cash flows, because cash only flows in after the client signs. If a client drags out the process - quite common in my experience - cash could dry up, especially in a start-up environment. Averages and smoothing techniques do not apply because closing is a binary decision. Clients rarely pay before the contract is signed, and you can’t extrapolate future earnings to earlier dates.&lt;/p&gt;

&lt;p&gt;I came up with three ways to build the model:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;use HLOOKUPs and a table&lt;/li&gt;
  &lt;li&gt;use user-defined functions (UDFs)&lt;/li&gt;
  &lt;li&gt;use R1C1 references&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first option used a complicated formula which would be difficult for investors and the financial team to parse. The second option required auditors, investors, and the financial team to be able to read VBA, which is, unfortunately, not a very common skill. The third option allowed me to make very clear references but still automate filling out the sheet.&lt;/p&gt;

&lt;p&gt;To be transparent, we did end up using HLOOKUP, even though it was messy, because it allowed investors and auditors to easily perform what-if analyses with an .xlsx file instead of an .xlsm file, which are often blocked on internal bank computers. That said, for unsophisticated investors and financial teams, using R1C1 references populated by VBA is still an illustrative approach.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;thesetup&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;The Setup&lt;/h3&gt;
&lt;p&gt;Our sheet was set up like in the image below (I’ve changed details for confidentiality). The top section is the number of expected leads, based on marketing, networking, etc. The next section is how many of the leads are expected to close. And the last section provides the expected close time (in months) and the actual number of clients.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2022-01-21-vba-formular1c1-client-leads.PNG&quot; alt=&quot;Client leads, close rates, and times&quot; title=&quot;Client leads, close rates, and times&quot; /&gt;&lt;br /&gt;&lt;b&gt;Client leads, close rates, and times&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;thecode&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;The Code&lt;/h3&gt;
&lt;p&gt;Now for the code. The code essentially enters a formula in each cell in the range &lt;kbd&gt;C18:Z22&lt;/kbd&gt; that references the close rate and the client leads from a specific number of months ago.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;populateClientLeads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;monthsToClose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currMonth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clientType&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;For&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clientType&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;To&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;&#39;iterates through all 5 client types&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;monthsToClose&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Cells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clientType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;&#39;grabs number of months to close for current client type&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;For&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currMonth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;To&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;23&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;&#39;starts from 0, January 2022, but note the Cells reference must target the correct cell&lt;/span&gt;
    
        &lt;span class=&quot;k&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currMonth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;monthsToClose&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;&#39;move current month forward in time until close month makes sense (close must be greater than current month)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;Do&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Until&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currMonth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;monthsToClose&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;currMonth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currMonth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;Loop&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;If&lt;/span&gt;
    
        &lt;span class=&quot;n&quot;&gt;Cells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clientType&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currMonth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FormulaR1C1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;=R[-8]C2 * R[-15]C[-&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;monthsToClose&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;]&quot;&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;&#39;start in row 18 and move down for each successive client type&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;&#39;start in column 3 and add the current month&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;&#39;first R1C1 references the close rates, and notice how the column is locked&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;&#39;second R1C1 looks 15 rows prior to get client leads for that client type then&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;&#39;moves back a number of columns equal to the close time&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;currMonth&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Next&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clientType&lt;/span&gt;

        
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a longer code block than we often post here, so here’s an example to help clarify.&lt;/p&gt;

&lt;p&gt;Energy companies take 3 months to close, so to see how many energy companies actually sign contracts in June 2022, we should look at March 2022 leads, 3 months earlier than June.&lt;/p&gt;

&lt;p&gt;Of course, we also need to incorporate the close rate (obviously not all leads close), and we must lock the close rate column, since this macro populates the entire range &lt;kbd&gt;C18:Z22&lt;/kbd&gt;. The reason we reference the close rate with R1C1 style and explicitly add it to the formula is so that investors and auditors can inspect the formula themselves.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;theresult&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;The Result&lt;/h3&gt;
&lt;p&gt;The result is a fully-populated client sales section with a very simple-to-read formula. Investors and auditors can click on any cell in &lt;kbd&gt;C18:Z22&lt;/kbd&gt; and see exactly how it’s calculated. It is much more concise than trying to understand HLOOKUPs and VBA code (for the investors and auditors, at least).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2022-01-21-vba-formular1c1-sales-populated.PNG&quot; alt=&quot;Populated sales with proper timing&quot; title=&quot;Populated sales with proper timing&quot; /&gt;&lt;br /&gt;&lt;b&gt;Populated sales with proper timing&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;In this screenshot, the cell &lt;kbd&gt;S18&lt;/kbd&gt; contains a formula that looks at the &lt;mod&gt;Banking / Finance&lt;/mod&gt; close rate and checks the &lt;mod&gt;Banking / Finance&lt;/mod&gt; leads 6 months prior to &lt;kbd&gt;S18&lt;/kbd&gt; (May 2023), which is November 2022. Manually adjusting each time the average close time changes, especially when doing what-if financial analyses, would be very cumbersome. With the macro, you change the average close time, click a macro button, and the formulas are all rewritten. Unfortunately for my application, that does require an .xlsm file, which couldn’t be run on the investors’ and auditors’ systems. I’m sure many of you can relate to that.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Now you can use R1C1-style notation and even lock columns and rows with it, use variable relative references, and apply cell-distance logic to formulas. R1C1 notation is easy to work with once you’re familiar with it and it makes any distance-based manipulations very easy in your VBA macros.&lt;/p&gt;

&lt;p&gt;For more VBA tips and tricks like this, subscribe using the form below.&lt;/p&gt;
</description>
        <pubDate>Fri, 21 Jan 2022 10:00:00 +0000</pubDate>
        <media:content url="https://wellsr.com/vba/assets/images/2022-01-21-vba-formular1c1-tutorial.png" medium="image"></media:content>
        <link>https://wellsr.com/vba/2022/excel/vba-formular1c1/</link>
        <guid isPermaLink="true">https://wellsr.com/vba/2022/excel/vba-formular1c1/</guid>
        
        
        <category>excel</category>
        
      </item>
    
      <item>
        <title>Use VBA Range.Find to Search and Retrieve Data</title>
        <description>&lt;p&gt;We’ve all searched Excel via the GUI for values and strings. Usually we’re just looking for a single cell and then we go on our way, but you can also code the search behavior in VBA to automate the process. This is useful when building customized dashboards, a specific algorithm or a repetitive find and replace process.&lt;/p&gt;

&lt;p&gt;The key to using VBA to find matches in cells is &lt;code&gt;Range.Find&lt;/code&gt;, a method that searches the range and finds what you’re looking for - with plenty of options, too.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#methodandrange&quot;&gt;The Method and the Range &lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#test&quot;&gt;An Essential Test &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#whyuse&quot;&gt;Why Use &lt;code&gt;.Find&lt;/code&gt; at all? &lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#params&quot;&gt;Range.Find Parameters &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#usingthereturn&quot;&gt;Using the Return Value &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Finding all matches&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;methodandrange&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;The Method and the Range&lt;/h2&gt;
&lt;p&gt;First, the &lt;code&gt;Range.Find&lt;/code&gt; method is a method that acts on a &lt;strong&gt;Range&lt;/strong&gt; object. Therefore, you must know which part of which sheets and workbook you want to target. Set the range with a start and end cell or use an entire column. We’re going to slowly build our &lt;code&gt;Find&lt;/code&gt; function, starting with ways to specify the range you want to search.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A2:D500&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Find&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;&#39;starts from Row2/Col1 and goes to Row500/Col4&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:D&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Find&lt;/span&gt;           &lt;span class=&quot;c1&quot;&gt;&#39;starts in Row1/Col1 and goes to the final row in Col4&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Using an entire column means you don’t have to specify the starting or ending rows - they’ll all be included.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;test&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;An Essential Test&lt;/h3&gt;
&lt;p&gt;Since the return value is either &lt;code&gt;Nothing&lt;/code&gt; or a Range object (it will be a cell), and we usually want to access properties of the object, it makes sense to set the output to an object, like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;foundCell&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Find&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then we can conveniently access the properties of &lt;mod&gt;foundCell&lt;/mod&gt;, like the &lt;code&gt;.Address&lt;/code&gt;, &lt;code&gt;.Row&lt;/code&gt; or even its &lt;code&gt;.Value&lt;/code&gt;. However, because the output is &lt;code&gt;Nothing&lt;/code&gt; when there are no matches, we must check for this condition before attempting to access these properties:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;k&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;Is&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;&#39;code to handle not finding a match&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Else&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;&#39;code to handle the match, such as foundRow = foundCell.Row&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;If&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;

&lt;p style=&quot;margin-bottom:10px;&quot;&gt;&lt;span style=&quot;font-size:26px;color:#000000;&quot;&gt;Make powerful macros with our free VBA Developer Kit&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;padding-top:10px;&quot;&gt;&lt;p&gt;This is actually pretty neat. If you have trouble understanding or remembering it, our free &lt;b&gt;VBA Developer Kit&lt;/b&gt; can help. It’s loaded with VBA shortcuts to help you make your own macros like this one - we’ll send a copy, along with our &lt;b&gt;Big Book of Excel VBA Macros&lt;/b&gt;, to your email address below.&lt;/p&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;centered&quot;&gt;
&lt;form action=&quot;//wellsr.us13.list-manage.com/subscribe/post?u=9fa014ad47345eb4b70403d38&amp;amp;id=5c804a55ec&quot; method=&quot;post&quot; class=&quot;form-inline validate&quot; target=&quot;_blank&quot; id=&quot;email-subscribe&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-md-10 col-md-offset-1&quot;&gt;&lt;input required=&quot;&quot; type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;consent&quot; id=&quot;mce-Checkbox&quot; /&gt; I&#39;ll take a free VBA Developer Kit&lt;br /&gt;&lt;/div&gt;
&lt;input type=&quot;email&quot; value=&quot;&quot; name=&quot;EMAIL&quot; class=&quot;form-control email&quot; id=&quot;mce-EMAIL&quot; placeholder=&quot;Enter your email address&quot; required=&quot;&quot; /&gt;
&lt;div class=&quot;mc-field-group input-group&quot; style=&quot;display:none&quot;&gt;
    &lt;strong&gt;Programming Language &lt;/strong&gt;
    &lt;ul&gt;&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;1&quot; name=&quot;group[17657][1]&quot; id=&quot;mce-group[17657]-17657-0&quot; checked=&quot;&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-0&quot;&gt;VBA&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;group[17657][2]&quot; id=&quot;mce-group[17657]-17657-1&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-1&quot;&gt;Python&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups--&gt;
&lt;div style=&quot;position: absolute; left: -5000px;&quot; aria-hidden=&quot;true&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;b_9fa014ad47345eb4b70403d38_5c804a55ec&quot; tabindex=&quot;-1&quot; value=&quot;&quot; /&gt;&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-success btn-lg&quot; name=&quot;subscribe&quot; id=&quot;mc-embedded-subscribe&quot;&gt;Get my free kit&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;!--End mc_embed_signup--&gt;
&lt;/div&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;whyuse&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Why Use &lt;code&gt;.Find&lt;/code&gt; at all?&lt;/h3&gt;
&lt;p&gt;The first reason to use &lt;code&gt;.Find&lt;/code&gt; is efficiency. Built-in methods are almost always more efficient than the programmer’s code because Microsoft’s team has specifically optimized their calculations. Could you write a for-loop to look in your range and find the needed values? Sure. You could also read the values into an array. But these are not as efficient as using the built-in VBA method designed specifically to search the range.&lt;/p&gt;

&lt;p&gt;Another reason to use &lt;code&gt;.Find&lt;/code&gt; is to automate a process. Using the GUI to find cell locations is fine if you only have a few cells to check. But what if you have thousands and need to write all those locations into another file (using &lt;a href=&quot;/vba/2018/excel/read-append-or-write-to-text-files-with-vba-opentextfile/&quot;&gt;OpenTextFile&lt;/a&gt;)? &lt;code&gt;.Find&lt;/code&gt; will let you automate this process.&lt;/p&gt;

&lt;p&gt;Finally, while the Excel GUI is well-designed and powerful, you may be building a custom dashboard. To align the layouts and styles, you may want to build your own search GUI. You can still leverage the efficiency of the &lt;code&gt;.Find&lt;/code&gt; method on the backend while your users see your custom frontend UserForm.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;params&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Range.Find Parameters&lt;/h2&gt;
&lt;p&gt;So far we’ve only presented pseudo-code to help paint the big picture of what a Range.Find macro should include (specifying the range, setting the output to a range variable, and checking for nothingness). The Range.Find method itself accepts quite a few inputs but only requires one: &lt;code&gt;What&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#What&quot;&gt;What&lt;/a&gt; - the thing you’re looking for. This accepts strings but also numbers. Note that operators (&amp;lt;, &amp;gt;, =, etc.) are &lt;strong&gt;not&lt;/strong&gt; accepted unless part of a string&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#After&quot;&gt;After&lt;/a&gt; - starts searching &lt;strong&gt;after&lt;/strong&gt; this cell (and excludes this cell until it loops around)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#LookIn&quot;&gt;LookIn&lt;/a&gt; - search the values (&lt;code&gt;xlValues&lt;/code&gt;) or formulas themselves (&lt;code&gt;xlFormulas&lt;/code&gt;). Sometimes used for comments, too (&lt;code&gt;xlComments&lt;/code&gt;, &lt;code&gt;xlCommentsThreaded&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#LookAt&quot;&gt;LookAt&lt;/a&gt; - used to match part of the cell (&lt;code&gt;xlPart&lt;/code&gt;) or force a full match (&lt;code&gt;xlWhole&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#SearchOrder&quot;&gt;SearchOrder&lt;/a&gt; - search an entire row (&lt;code&gt;xlByRows&lt;/code&gt;) or an entire column (&lt;code&gt;xlByColumns&lt;/code&gt;) first before moving to the next&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#SearchDirection&quot;&gt;SearchDirection&lt;/a&gt; - &lt;code&gt;xlPrevious&lt;/code&gt; or &lt;code&gt;xlNext&lt;/code&gt;, to navigate away from the &lt;code&gt;After&lt;/code&gt; cell&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#Match&quot;&gt;MatchCase&lt;/a&gt; - whether the case must be a perfect case-sensitive match or not (True or False)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#Match&quot;&gt;MatchByte&lt;/a&gt; - matching by character type (True or False)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#SearchFormat&quot;&gt;SearchFormat&lt;/a&gt; - True or False to search in cell formats and requires a separate line of code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; VBA saves your settings for LookIn, LookAt, and SearchOrder on every run, including by the GUI. Since users who interact with the GUI may make changes, good coding practice dictates hardcoding these parameters every time you use them in your macro.&lt;/p&gt;

&lt;h3 id=&quot;What&quot;&gt;What&lt;/h3&gt;
&lt;p&gt;Finding strings is straightforward. Simply enclose the necessary string in quotes. You can even use this to find parts of formula. For example, Column G may contain a user’s total purchases and a formula may exist, like &lt;mod&gt;If($G$1&amp;gt;3500,&quot;VIP&quot;,&quot;&quot;)&lt;/mod&gt;. To raise the VIP status threshold, you need to find the cells with that formula, first. Just pass part of the formula into the &lt;code&gt;What&lt;/code&gt; parameter.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;What&lt;/code&gt; can also be a number. One example is to find order numbers in a list and use that to produce the associated customer name, using something like &lt;a href=&quot;/vba/2016/excel/offset-vba-property-to-navigate-excel/&quot;&gt;VBA Offset&lt;/a&gt;. This could also be used to find part numbers in a big sheet of part IDs.&lt;/p&gt;

&lt;h3 id=&quot;After&quot;&gt;After&lt;/h3&gt;
&lt;p&gt;The search begins AFTER this argument. This argument should also be a cell, such as &lt;mod&gt;Cells(2,3)&lt;/mod&gt; or &lt;mod&gt;Range(&quot;C2&quot;)&lt;/mod&gt;. This will exclude C2 but include D2 and C4 in the searchable range.&lt;/p&gt;

&lt;h3 id=&quot;LookIn&quot;&gt;LookIn&lt;/h3&gt;
&lt;p&gt;This lets you choose whether you want to match the cell value, the cell formula, or the comments associated with the cell.&lt;/p&gt;

&lt;p&gt;Most commonly we use &lt;code&gt;xlValues&lt;/code&gt;, but we may want to find a formula so that we can change it. This also becomes important to set if we have cells that dictate text via formula, such as &lt;mod&gt;=if(G&amp;gt;1500, &quot;VIP&quot;,&quot;Normal&quot;)&lt;/mod&gt;. If we want to find VIP clients (not the &lt;code&gt;&quot;VIP&quot;&lt;/code&gt; in the code) we must pass &lt;code&gt;xlValues&lt;/code&gt; to &lt;code&gt;LookIn&lt;/code&gt;. If we passed in (or used a saved setting) &lt;code&gt;xlFormulas&lt;/code&gt;, we’d get each cell containing this IF-statement.&lt;/p&gt;

&lt;h3 id=&quot;LookAt&quot;&gt;LookAt&lt;/h3&gt;
&lt;p&gt;This lets you match part of or the whole cell value (or formula or comment).&lt;/p&gt;

&lt;p&gt;For example, you may have multiple words in a cell (or very likely in comments). If your code can only captures a single word (perhaps a user can only remember part of the comment), then you can use &lt;code&gt;xlPart&lt;/code&gt; to find any partial match.&lt;/p&gt;

&lt;p&gt;If you’d like, you can insert wildcard characters &lt;code&gt;*&lt;/code&gt; into the string to effectively induce &lt;code&gt;xlPart&lt;/code&gt; behavior with &lt;code&gt;xlWhole&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:K&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;What&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;check&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LookIn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlComments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LookAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlWhole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;&#39;the entire comment must be the single word check&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:K&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;What&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;check&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LookIn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlComments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LookAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlPart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;            &lt;span class=&quot;c1&quot;&gt;&#39;find all comments with &quot;check&quot; in them&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:K&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;What&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;*check*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LookIn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlComments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LookAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlWhole&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;functionally equivalent to immediately preceding statement&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id=&quot;SearchOrder&quot;&gt;SearchOrder&lt;/h3&gt;
&lt;p&gt;Use this argument to choose to search by columns or by rows. For some applications this is not very useful, but in others, it may be essential.&lt;/p&gt;

&lt;p&gt;When you have multiple cells with similar entries, especially if you are using &lt;code&gt;xlPart&lt;/code&gt;, you may have multiple hits. You may also want to exhaust an entire column or row before moving to the next. You could achieve this by using one line of code to target each row or column, but that is unwieldy. Setting &lt;code&gt;SearchOrder&lt;/code&gt; is much more efficient.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-12-10-vba-range-find-searchorder.PNG&quot; alt=&quot;VBA Range.Find Search Order&quot; title=&quot;VBA Range.Find Search Order&quot; /&gt;&lt;br /&gt;&lt;b&gt;Search down the rows or across the columns&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;With the image above in mind, when you set &lt;code&gt;xlByRows&lt;/code&gt;, you will check each cell in Column A, and only when the data in Column A is exhausted will &lt;code&gt;Range.Find&lt;/code&gt; move to Column B. Conversely, setting &lt;code&gt;xlByColumns&lt;/code&gt; will look across the columns, starting in Row 1 and continue until all columns are exhausted before moving to Row 2.&lt;/p&gt;

&lt;h3 id=&quot;SearchDirection&quot;&gt;SearchDirection&lt;/h3&gt;
&lt;p&gt;This parameter sets whether Excel moves down and to the right or up and to the left from your starting cell. If you haven’t set &lt;code&gt;After&lt;/code&gt;, then the upper-left corner of the range marks the starting point. Knowing this is useful when you’re running &lt;code&gt;.Find&lt;/code&gt; in a loop.&lt;/p&gt;

&lt;p&gt;Remember that this parameter’s choice is saved at the application level, so when you run another &lt;code&gt;Range.Find&lt;/code&gt; line, whether in another part of the code or as part of a loop, Excel will continue in the same direction.&lt;/p&gt;

&lt;h3 id=&quot;Match&quot;&gt;MatchCase and MatchByte&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;MatchCase&lt;/code&gt; toggles case matching. This might be beneficial if your data formatting standards rely on case, such as all-caps for headers. Using &lt;code&gt;UCase&lt;/code&gt; and the header names would help you identify header rows in a large sheet. It could also be used to find “important” comments where the commenter has written everything in capital letters.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;MatchByte&lt;/code&gt; is used for matching characters, specifically of languages that have too many characters for single-byte data formats. English and other European languages can encode their alphabets using one byte (8 bits) of data. For some languages, like Mandarin, where an alphabet doesn’t directly encode the language’s sounds in an alphabet, more bytes are needed, and hence the use of &lt;code&gt;MatchByte&lt;/code&gt; could be beneficial.&lt;/p&gt;

&lt;p&gt;Both of these properties are technically of &lt;code&gt;Variant&lt;/code&gt; type, but they use True and False, just like a Boolean data type.&lt;/p&gt;

&lt;h3 id=&quot;SearchFormat&quot;&gt;SearchFormat&lt;/h3&gt;
&lt;p&gt;This parameter lets you search formatting, such as bold or large font, for key information. You could even use it for cell fill colors, like if you highlight all &lt;mod&gt;Total&lt;/mod&gt; rows with a yellow background.&lt;/p&gt;

&lt;p&gt;Don’t forget to clear the &lt;code&gt;FindFormat&lt;/code&gt; format before starting, just in case.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FindFormat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Clear&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FindFormat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Interior&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vbYellow&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:K&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;What&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SearchFormat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you don’t know the text for a cell (in value, comment, or formula), you can search solely on formatting by using the wildcard, which will match any text (excluding empty cells). Searching an empty string will help you find the blank cells.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;usingthereturn&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Using the Return Value&lt;/h2&gt;
&lt;p&gt;If a match exists, Excel will return a cell as a &lt;strong&gt;Range&lt;/strong&gt; object. With this, you can access any property that a range would reasonably have.&lt;/p&gt;

&lt;p&gt;One example is substituting &lt;code&gt;.Find&lt;/code&gt; as a database relation and using the input as a kind of foreign key. Let’s look at an example.&lt;/p&gt;

&lt;p&gt;Say you have one sheet with orders, names, and customer IDs, and a second but password-protected sheet that stores customer details. Moreover, you have three access levels for employees: level-1 sees only orders, level-2 sees orders and customer phone numbers, and level-3 sees complete customer and order details.&lt;/p&gt;

&lt;p&gt;In this case you may use a VLOOKUP to display names next to orders, linking them by customer ID, but you don’t want to display customer phone numbers to level-1 employees. In this case, you could create a password-enabled dashboard, then use &lt;code&gt;.Find&lt;/code&gt; to output the customer’s phone number based on the ID. This would give level-2 employees a way to access phone numbers but protect further details, like customer addresses.&lt;/p&gt;

&lt;p&gt;This pseudocode makes &lt;code&gt;orderNum&lt;/code&gt; act a bit like a foreign key in a database:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;orderNum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;functionToGetOrderNumber&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sheets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Customer Details&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;customer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;details&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;orderNum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;customerPhone&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sheets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Customer Details&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Cells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;customerPhoneColumn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;conclusion&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Finding all matches&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Range.Find&lt;/code&gt; can be as simple as returning the first match of a string, but the optional arguments available make the method very versatile, too. Strings are permissible but so are other data types, and you can search through cell values but also cell formulas and even comments. The option to search for a specific format works, too, and using the wildcard for &lt;code&gt;What&lt;/code&gt; here essentially converts &lt;code&gt;Range.Find&lt;/code&gt; into a format finder.&lt;/p&gt;

&lt;p&gt;You’ll notice that each instance of Range.Find only finds the next item. If you want to find all the cells matching your search criteria, you can combine &lt;code&gt;Range.Find&lt;/code&gt; with &lt;code&gt;Range.FindNext&lt;/code&gt; inside a loop and you can join all the found cells with the &lt;a href=&quot;/vba/2019/excel/use-vba-union-to-combine-ranges/&quot;&gt;VBA Union&lt;/a&gt; method.&lt;/p&gt;

&lt;p&gt;To help you build your own application using all of these methods, here’s a working code you can customize for your own needs:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;FindAll&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;foundCell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;&#39;single cell&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;foundCells&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;&#39;all found cells&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;searchCells&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;&#39;range you want to search&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;celladdress&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;just used so you know when you&#39;ve found everything&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;searchCells&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:E&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;searchCells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;What&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;check&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LookIn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlValues&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LookAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlPart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;span class=&quot;k&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;Is&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;&#39;code to handle not finding a match&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;no match found&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Else&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;&#39;find all other matching cells with FindNext&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;celladdress&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCells&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;Do&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;searchCells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FindNext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;Set&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCells&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Union&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foundCells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;combine found cells&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;Loop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;While&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;celladdress&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;foundCell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;Debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Print&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foundCells&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;If&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;At the end of this routine, all cells containing your search parameter will be stored in the Range object &lt;mod&gt;foundCells&lt;/mod&gt;. If you find this helpful and you want more VBA ideas, enter your email address using the form below. We put together some great resources to make sure you’re getting the most out of Excel VBA.&lt;/p&gt;

</description>
        <pubDate>Fri, 10 Dec 2021 10:00:00 +0000</pubDate>
        <media:content url="https://wellsr.com/vba/assets/images/2021-12-10-vba-range-find.png" medium="image"></media:content>
        <link>https://wellsr.com/vba/2021/excel/vba-range-find/</link>
        <guid isPermaLink="true">https://wellsr.com/vba/2021/excel/vba-range-find/</guid>
        
        
        <category>excel</category>
        
      </item>
    
      <item>
        <title>VBA AdvancedFilter with Multiple Criteria</title>
        <description>&lt;p&gt;The &lt;code&gt;AdvancedFilter&lt;/code&gt; method in VBA is a powerful tool we first introduced to &lt;a href=&quot;/vba/2021/excel/vba-filter-unique-values/&quot;&gt;get a list of unique values&lt;/a&gt;, which is common in applications that deal with large amounts of data. In this tutorial we’ll explore how to use &lt;code&gt;AdvancedFilter&lt;/code&gt; to filter on &lt;strong&gt;multiple criteria&lt;/strong&gt; rather than just a single column of data.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#targetrange&quot;&gt;The Target Range &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#criteriarange&quot;&gt;The Criteria Range &lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#multiplecriteria&quot;&gt;Filtering on Multiple Criteria &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#andvsor&quot;&gt;AdvancedFilter with AND vs OR &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#intervals&quot;&gt;AdvancedFilter with Intervals&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#outputrange&quot;&gt;The Output Range &lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#xlfilterinplace&quot;&gt;Using &lt;code&gt;XlFilterInPlace&lt;/code&gt; &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#xlfiltercopy&quot;&gt;Using &lt;code&gt;XlFilterCopy&lt;/code&gt; &lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#clearrange&quot;&gt;Clear Your &lt;code&gt;CopyToRange&lt;/code&gt; &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#takingitfurther&quot;&gt;Taking it Further &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;targetrange&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Target Range&lt;/h2&gt;
&lt;p&gt;We’ll use the same dataset we used in our article for finding unique values. Grab &lt;a href=&quot;/vba/assets/files/vba-filter-column-with-autofilter.csv&quot;&gt;the CSV here&lt;/a&gt; or follow along with this screenshot:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-09-03-vba-filter-column-with-autofilter-dataset.png&quot; alt=&quot;Names, Locations and Order Amounts&quot; title=&quot;Names, Locations and Order Amounts&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Here we have a list of orders with headers and the associated names, region, US state, and amount.&lt;/p&gt;

&lt;p&gt;Targeting the range is as simple as inputting the columns. VBA will treat everything in that column as part of the table. However, if you’d prefer to only filter part of a table, you can explicitly define an ending row when defining your range.&lt;/p&gt;

&lt;p&gt;In our case, rows &lt;mod&gt;A&lt;/mod&gt; through &lt;mod&gt;G&lt;/mod&gt; are the table, so we can target the range to be filtered. Our base range definition will look like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;criteriarange&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Criteria Range&lt;/h2&gt;
&lt;p&gt;Rather than take a single criterion, &lt;code&gt;AdvancedFilter&lt;/code&gt; considers an entire set of criteria.&lt;/p&gt;

&lt;p&gt;So we don’t have to hardcode our criteria in our VBA macro, let’s build a new table with headers matching those in our target range. Then, we’ll add our filtering requirements to this table. Columns &lt;mod&gt;I&lt;/mod&gt; and &lt;mod&gt;J&lt;/mod&gt; show our new table, below.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-11-10-vba-advancedfilter-fewer-criteria.PNG&quot; alt=&quot;Screenshot of data with just two criteria&quot; title=&quot;Screenshot of data with just two criteria&quot; /&gt;&lt;br /&gt;&lt;b&gt;Screenshot of data with just two criteria&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;You don’t need to cover every header and you don’t have to assign a criteria to each header in the table. Our target Range has 7 fields while our Criteria Range table only has &lt;mod&gt;City&lt;/mod&gt; and &lt;mod&gt;Amount&lt;/mod&gt;, and we didn’t even enter a restriction for the latter.&lt;/p&gt;

&lt;p&gt;We can then set our &lt;code&gt;CriteriaRange&lt;/code&gt; attribute to the range &lt;mod&gt;I1:J2&lt;/mod&gt;, like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CriteriaRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I1:J2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We’re still building our &lt;code&gt;AdvancedFilter&lt;/code&gt; statement, so the code won’t work just yet. If you try running it now, you’ll get a &lt;strong&gt;Run-time error ‘1004’: AdvancedFilter method of Range class failed&lt;/strong&gt; error because we haven’t defined an &lt;code&gt;Action&lt;/code&gt;. This argument tells &lt;code&gt;AdvancedFilter&lt;/code&gt; whether to filter in place or copy the filtered results to a new range. We’ll do that later, but I want to step through the code so far.&lt;/p&gt;

&lt;hr /&gt;

&lt;p style=&quot;margin-bottom:10px;&quot;&gt;&lt;span style=&quot;font-size:26px;color:#000000;&quot;&gt;Make powerful macros with our free VBA Developer Kit&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;padding-top:10px;&quot;&gt;&lt;p&gt;It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free &lt;b&gt;VBA Developer Kit&lt;/b&gt; and wrote the &lt;b&gt;Big Book of Excel VBA Macros&lt;/b&gt; full of hundreds of pre-built macros to help you master file I/O, arrays, strings and more - grab your free copy below.&lt;/p&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;centered&quot;&gt;
&lt;form action=&quot;//wellsr.us13.list-manage.com/subscribe/post?u=9fa014ad47345eb4b70403d38&amp;amp;id=5c804a55ec&quot; method=&quot;post&quot; class=&quot;form-inline validate&quot; target=&quot;_blank&quot; id=&quot;email-subscribe&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-md-10 col-md-offset-1&quot;&gt;&lt;input required=&quot;&quot; type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;consent&quot; id=&quot;mce-Checkbox&quot; /&gt; I&#39;ll take a free VBA Developer Kit&lt;br /&gt;&lt;/div&gt;
&lt;input type=&quot;email&quot; value=&quot;&quot; name=&quot;EMAIL&quot; class=&quot;form-control email&quot; id=&quot;mce-EMAIL&quot; placeholder=&quot;Enter your email address&quot; required=&quot;&quot; /&gt;
&lt;div class=&quot;mc-field-group input-group&quot; style=&quot;display:none&quot;&gt;
    &lt;strong&gt;Programming Language &lt;/strong&gt;
    &lt;ul&gt;&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;1&quot; name=&quot;group[17657][1]&quot; id=&quot;mce-group[17657]-17657-0&quot; checked=&quot;&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-0&quot;&gt;VBA&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;group[17657][2]&quot; id=&quot;mce-group[17657]-17657-1&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-1&quot;&gt;Python&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups--&gt;
&lt;div style=&quot;position: absolute; left: -5000px;&quot; aria-hidden=&quot;true&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;b_9fa014ad47345eb4b70403d38_5c804a55ec&quot; tabindex=&quot;-1&quot; value=&quot;&quot; /&gt;&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-success btn-lg&quot; name=&quot;subscribe&quot; id=&quot;mc-embedded-subscribe&quot;&gt;Get my free kit&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;!--End mc_embed_signup--&gt;
&lt;/div&gt;
&lt;hr /&gt;

&lt;p&gt;What we’ve asked &lt;code&gt;AdvancedFilter&lt;/code&gt; to do so far is filter all entries in &lt;mod&gt;Range(&quot;A:G&quot;)&lt;/mod&gt; that contain &lt;mod&gt;Pittsburgh&lt;/mod&gt;. This is a simple single-column filter on &lt;mod&gt;City&lt;/mod&gt;.&lt;/p&gt;

&lt;p&gt;The range contains &lt;strong&gt;the headers/fields and the criteria themselves&lt;/strong&gt;. Notice that, unlike the Target Range, &lt;strong&gt;we don’t&lt;/strong&gt; enter the whole column. A blank &lt;strong&gt;row&lt;/strong&gt; in the Criteria Range will match all data records, which is not what we want. Conversely, Excel interprets a blank &lt;strong&gt;cell&lt;/strong&gt; (here &lt;mod&gt;J2&lt;/mod&gt;) to mean any value. Since &lt;mod&gt;J2&lt;/mod&gt; is empty here, all amounts are valid.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;multiplecriteria&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Filtering on Multiple Criteria&lt;/h3&gt;
&lt;p&gt;This is where the power of &lt;code&gt;AdvancedFilter&lt;/code&gt; becomes clear. Let’s say we want to analyze all &lt;mod&gt;Center&lt;/mod&gt; Region orders over $400. We’ll set up our filter table (Criteria Range) to give us the flexibility to drill down by &lt;mod&gt;City&lt;/mod&gt;, too.&lt;/p&gt;

&lt;p&gt;We can set up the Criteria Range like so:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-11-10-vba-advancedfilter-region-amount-city.PNG&quot; alt=&quot;AdvancedFilter Criteria Range with Region, Amount and City&quot; title=&quot;AdvancedFilter Criteria Range with Region, Amount and City&quot; /&gt;&lt;/p&gt;

&lt;p&gt;To set this as the criteria range, we should expand to Column K, like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CriteriaRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I1:K2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With this single table, we can choose to filter on &lt;mod&gt;Region&lt;/mod&gt;, &lt;mod&gt;City&lt;/mod&gt; or both.&lt;/p&gt;

&lt;p&gt;We can also have multiple sets of criteria. We can investigate the Center Region with Amounts greater than $400 OR the West Region with Amounts greater than $300.&lt;/p&gt;

&lt;p&gt;To add a second set of criteria, we simply add it to a new row and make sure to expand our Criteria Range in our macro:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-11-10-vba-advancedfilter-two-region-amount-city.PNG&quot; alt=&quot;AdvancedFilter Criteria Range with Two Regions, Amounts and Cities&quot; title=&quot;AdvancedFilter Criteria Range with Two Regions, Amounts and Cities&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CriteriaRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I1:K3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;include Row 3&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice we still don’t have an &lt;code&gt;Action&lt;/code&gt; argument defined so this macro still won’t work yet. Don’t worry, we’re getting closer.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;andvsor&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;AdvancedFilter with AND vs OR&lt;/h3&gt;
&lt;p&gt;In computing&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;AND means all conditions must be met&lt;/li&gt;
  &lt;li&gt;OR means at least one condition must be met&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With &lt;code&gt;AdvancedFilter&lt;/code&gt; criteria ranges, across rows are treated as ANDs while down rows are treated as ORs.&lt;/p&gt;

&lt;p&gt;Thus,the previous example was&lt;/p&gt;

&lt;pre&gt;
Center AND &amp;gt;400

OR

West AND &amp;gt;300
&lt;/pre&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;intervals&quot;&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;AdvancedFilter with Intervals&lt;/h3&gt;

&lt;p&gt;With the mathematical operators (&amp;lt;, &amp;gt;, etc.), you can select intervals, too:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-11-10-vba-advancedfilter-amount-range.PNG&quot; alt=&quot;VBA AdvancedFilter with Intervals&quot; title=&quot;VBA AdvancedFilter with Intervals&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CriteriaRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I1:L3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This one will filter for&lt;/p&gt;

&lt;pre&gt;
Center AND &amp;gt;$400 

OR 

West AND &amp;gt;$300 AND &amp;lt;$400
&lt;/pre&gt;

&lt;p&gt;It’s important to remember ANDs go across rows and ORs go down rows when defining your criteria range tables. If you’re not careful, you could end up with statements like &lt;em&gt;West AND Center AND &amp;gt;$100&lt;/em&gt;, which wouldn’t return any records since an order can only be assigned to one &lt;mod&gt;Region&lt;/mod&gt;.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;outputrange&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Output Range&lt;/h2&gt;
&lt;p&gt;The only mandatory parameter, &lt;code&gt;Action&lt;/code&gt;, has two possible values: &lt;code&gt;XlFilterInPlace&lt;/code&gt; and &lt;code&gt;XlFilterCopy&lt;/code&gt;. You must define one of these &lt;code&gt;XlFilterAction&lt;/code&gt;s or your AdvancedFilter will fail with a run-time 1004 error.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;xlfilterinplace&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Using &lt;code&gt;XlFilterInPlace&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;XlFilterInPlace&lt;/code&gt; action filters the entire Target Range by hiding rows directly in your table. You’ll get all the fields in the output. Let’s use our criteria above for&lt;/p&gt;

&lt;pre&gt;
Center AND &amp;gt;400

OR

West AND &amp;gt;300
&lt;/pre&gt;

&lt;p&gt;We can filter in place with this code using a macro like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AdvancedFilterDemo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlFilterInPlace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CriteriaRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I1:K3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-11-10-vba-advancedfilter-filter-in-place.PNG&quot; alt=&quot;Output FilterInPlace&quot; title=&quot;Output FilterInPlace&quot; /&gt;&lt;br /&gt;&lt;b&gt;Output FilterInPlace&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Unqualifying rows are now hidden, though that does include some of the rows in our Criteria Range table itseflf. This is a very quick way to filter, but it does have the drawback of potentially hiding important rows outside your main table.&lt;/p&gt;

&lt;p&gt;Note the duplicate Sun Tzu lines in rows 58 through 61, too. You could use &lt;code&gt;AdvancedFilter&lt;/code&gt; to display only one of these by leveraging the &lt;a href=&quot;/vba/2021/excel/vba-filter-unique-values/&quot;&gt;Unique parameter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To unhide all the rows, run this code&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;ActiveSheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ShowAllData&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Once you do that, you’ll be back to your original state.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;xlfiltercopy&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Using &lt;code&gt;XlFilterCopy&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;In many instances it’s better to copy the output to a new location. This can be another sheet or simply another location on the same sheet.&lt;/p&gt;

&lt;p&gt;This method also provides more control over the output, because you can choose which fields to display.&lt;/p&gt;

&lt;p&gt;Let’s say you want the criteria&lt;/p&gt;

&lt;pre&gt;
Center AND &amp;gt;400

OR

West AND &amp;gt;300

OR 

Boston
&lt;/pre&gt;

&lt;p&gt;but you only want to output&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;family name&lt;/li&gt;
  &lt;li&gt;amount&lt;/li&gt;
  &lt;li&gt;order number&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We’ll start the output at Row 6 (light blue) under the same columns as the Criteria Range (light green). You’ll need to manually type out the header names you want in order for the AdvancedFilter macro to know which headers to paste. That’s precisely what we did in light blue, below. Once you type those out, the VBA AdvancedFilter method will know the columns of data you want and it’ll automatically copy the results matching your filter criteria to that location. If you don’t do this, you’ll get a &lt;strong&gt;Run-time error ‘1004’: The extract range has a missing or invalid field name&lt;/strong&gt; error.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-11-10-vba-advancedfilter-filtercopy.PNG&quot; alt=&quot;VBA AdvancedFilter with Multiple Criteria to New Table&quot; title=&quot;VBA AdvancedFilter with Multiple Criteria to New Table&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AdvancedFilterCopyDemo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Action&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlFilterCopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;CriteriaRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I1:K4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;CopyToRange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I6:K6&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The last parameter, &lt;code&gt;CopyToRange&lt;/code&gt;, contains the Range for the headers/fields you select.&lt;/p&gt;

&lt;p&gt;The Criteria Range and Output Range don’t really have to share fields at all. For example, we could filter for amounts greater than $500 in the North region and only output the order number. There’s nothing wrong with that. Of course, both ranges must share fields with the original table, otherwise &lt;code&gt;AdvancedFilter&lt;/code&gt; won’t know what to do.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;clearrange&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Clear Your &lt;code&gt;CopyToRange&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;If using &lt;code&gt;XlFilterCopy&lt;/code&gt;, Excel will guess what to clear in your Output Range. However, you can always be safer by clearing it yourself.&lt;/p&gt;

&lt;p&gt;Clear from the first row of data, not from the header row:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I7:K&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Clear&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;&#39;headers are in Row 6&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This method runs very fast and will clear every row of data all the way to the last row of your worksheet.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;takingitfurther&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Taking It Further&lt;/h2&gt;
&lt;p&gt;By combining &lt;code&gt;XlFilterCopy&lt;/code&gt; with multiple sheets, &lt;a href=&quot;/vba/vba-cheat-sheets/userforms/&quot;&gt;userforms&lt;/a&gt;, or even a &lt;a href=&quot;/vba/2021/excel/vba-usedrange/&quot;&gt;UsedRange&lt;/a&gt; (to identify the bounds of the Criteria Range and Output Range), you can create a very nice tool for your clients or your own data analysis needs.&lt;/p&gt;

&lt;p&gt;These steps give a high-level overview for a sophisticated AdvancedFilter tool:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Place the data table on one sheet&lt;/li&gt;
  &lt;li&gt;Put the user-adjustable Criteria Range on another sheet, using Data Validation to restrict headers to those in the table&lt;/li&gt;
  &lt;li&gt;Programmatically determine the last row in the Criteria Range sheet uisng &lt;a href=&quot;/vba/2015/excel/find-last-row-end-xlup/&quot;&gt;End(xlUp).Row&lt;/a&gt; or &lt;a href=&quot;/vba/2021/excel/vba-usedrange/&quot;&gt;UsedRange&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Allow users to change output fields under the direction of Data Validation&lt;/li&gt;
  &lt;li&gt;Add a button to the criteria page that runs your &lt;code&gt;AdvancedFilter&lt;/code&gt; macro&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;p&gt;Many businesses and organizations leverage Excel for its data manipulation capabilities and &lt;code&gt;AdvancedFilter&lt;/code&gt; is one of the quickest ways to get an overview of the data or at least extract the important bits.&lt;/p&gt;

&lt;p&gt;You can use the VBA &lt;code&gt;AdvancedFilter&lt;/code&gt; method to find matching fields, capture intervals in numerical data and adjust with AND/OR criteria range table structures.&lt;/p&gt;

&lt;p&gt;If you found this tutorial useful and you want more tips for writing powerful VBA macros, enter your email address below and I’ll give you my VBA Developer’s Kit for free.&lt;/p&gt;

</description>
        <pubDate>Fri, 05 Nov 2021 09:00:00 +0000</pubDate>
        <media:content url="https://wellsr.com/vba/assets/images/2021-11-05-vba-advancedfilter.png" medium="image"></media:content>
        <link>https://wellsr.com/vba/2021/excel/vba-advancedfilter-with-multiple-criteria/</link>
        <guid isPermaLink="true">https://wellsr.com/vba/2021/excel/vba-advancedfilter-with-multiple-criteria/</guid>
        
        
        <category>excel</category>
        
      </item>
    
      <item>
        <title>VBA Sgn Function to Check for Positive or Negative Numbers</title>
        <description>&lt;p&gt;Every once in a while, I like to post a tutorial about a VBA function that’s rarely used. This is one of those tutorials. The &lt;strong&gt;VBA Sgn function&lt;/strong&gt; is a quick way to check if a number is positive, negative or zero. It’s very simple and returns an integer based on the sign of the number you’re testing.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If the number is greater than zero, Sgn returns 1&lt;/li&gt;
  &lt;li&gt;If the number equals zero, Sgn returns 0&lt;/li&gt;
  &lt;li&gt;If the number is less than zero, Sgn returns -1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you try executing the Sgn function on a value that isn’t a number, like a string, you’ll get a type mismatch so it’s a good idea to pair it with the &lt;a href=&quot;/vba/2016/excel/isnumeric-vba-function-checks-if-cell-is-a-number/&quot;&gt;VBA IsNumeric function&lt;/a&gt;, which is what we’re going to do in the following example:&lt;/p&gt;

&lt;h2 id=&quot;vba-sgn-function-example-macro&quot;&gt;VBA Sgn Function Example Macro&lt;/h2&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;SgnFunctionDemo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;myval&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IsNumeric&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sgn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;MsgBox&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Cell A1 is positive&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;ElseIf&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sgn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;MsgBox&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Cell A1 is 0 or blank&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;ElseIf&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Sgn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;MsgBox&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Cell A1 is negative&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;If&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Else&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;MsgBox&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Cell A1 is not numeric&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;If&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In this macro, we grab the value from cell &lt;mod&gt;A1&lt;/mod&gt; and first check whether or not it’s numeric. This helps avoid type mismatch errors. After that, we determine the sign of the value using the VBA Sgn function in a series of conditional &lt;code&gt;If&lt;/code&gt; statements. You could use this structure to take different actions based on the sign of the cell, or even to force all values to be either negative or positive. Granted, you could just use the &lt;a href=&quot;/vba/2018/excel/vba-absolute-value-with-abs-function/&quot;&gt;VBA Abs function&lt;/a&gt; for that, but there’s always more than one way to do something in VBA.&lt;/p&gt;

&lt;p&gt;If you’re serious about learning VBA, consider subscribing using the form below. I’ll send a VBA tutorial each week to help you master both simple and challenging VBA concepts.&lt;/p&gt;
</description>
        <pubDate>Sat, 16 Oct 2021 07:00:00 +0000</pubDate>
        <media:content url="https://wellsr.com/vba/assets/images/2021-10-16-vba-sgn-function.png" medium="image"></media:content>
        <link>https://wellsr.com/vba/2021/excel/vba-sgn-function-check-positive-or-negative-numbers/</link>
        <guid isPermaLink="true">https://wellsr.com/vba/2021/excel/vba-sgn-function-check-positive-or-negative-numbers/</guid>
        
        
        <category>excel</category>
        
      </item>
    
      <item>
        <title>How to Filter a Column with VBA AutoFilter</title>
        <description>&lt;p&gt;Filtering hides rows from a table.  Filtering data is a foundational component in understanding large datasets. Some applications only need filtering on one column while others may be more complex. This article will explore filtering on a single column, but we have helpful tutorials on [applying multiple filters with VBA AutoFilter][how to apply multiple filters] and &lt;a href=&quot;/vba/2020/excel/vba-sort-columns/&quot;&gt;sorting columns using VBA&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#filterrange&quot;&gt;The Filter Range &lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#terminology&quot;&gt;A Word on Terminology &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#fieldparam&quot;&gt;The &lt;code&gt;Field&lt;/code&gt; Parameter &lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#definingcriteria&quot;&gt;Defining the Filter Criteria &lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#onestring&quot;&gt;Filter For a String &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#multiplestrings&quot;&gt;Filter For Multiple Strings &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#values&quot;&gt;Filter For Values &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#specialfilters&quot;&gt;Special Filters &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#userinput&quot;&gt;Leverage User Input &lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#clearingfilters&quot;&gt;Clearing Filters &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;filterrange&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Filter Range&lt;/h2&gt;
&lt;p&gt;The first thing to ask yourself when filtering is “what range should I filter?” The &lt;code&gt;.AutoFilter&lt;/code&gt; method &lt;em&gt;filters only one column at a time&lt;/em&gt;. To filter multiple columns requires an iteration loop or the use of the &lt;code&gt;.AdvancedFilter&lt;/code&gt; method, which we touched on in our article about &lt;a href=&quot;/vba/2021/excel/vba-filter-unique-values/&quot;&gt;filtering unique values&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.AutoFilter&lt;/code&gt; method operates on &lt;strong&gt;Range&lt;/strong&gt; objects, which both &lt;code&gt;Range&lt;/code&gt; and &lt;code&gt;Columns&lt;/code&gt; objects satisfy. Since we want to filter an entire column, we can use either of these lines:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;&#39;to filter Column 1 (same as A)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:A&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;&#39;to filter Column A (same as 1)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you want to filter a defined range rather than an entire column, specify so in your initial &lt;strong&gt;Range&lt;/strong&gt; object: &lt;code&gt;Range(&quot;A1:A300&quot;).AutoFilter&lt;/code&gt;. This allows you to filter up to a certain row, leaving other rows alone.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;terminology&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;A Word on Terminology&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Excel filters apply across &lt;em&gt;entire&lt;/em&gt; rows!&lt;/strong&gt; You cannot filter only some columns, because filtering hides the entire row. Filtering some columns but not others would break the integrity of the data, erasing the relational connections between columns.&lt;/p&gt;

&lt;p&gt;To explain what we mean, try visualizing this scenario. Let’s say we have First Names in column &lt;mod&gt;A&lt;/mod&gt; and Last Names in column &lt;mod&gt;B&lt;/mod&gt;. It wouldn’t make sense to filter only rows in &lt;mod&gt;A&lt;/mod&gt; because then first and last names would no longer be matched appropriately.&lt;/p&gt;

&lt;p&gt;Therefore, we usually speak of filtering ON a column, meaning we can look for all first names that start with J, but we cannot filter only Column &lt;mod&gt;A&lt;/mod&gt; without hiding values in &lt;mod&gt;B&lt;/mod&gt;. The phrase &lt;em&gt;filter on [target]&lt;/em&gt; implies we use the target as the criteria, but we still hide rows across the entire sheet.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;fieldparam&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;The &lt;code&gt;Field&lt;/code&gt; Parameter&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;.AutoFilter&lt;/code&gt; method has a parameter &lt;code&gt;Field&lt;/code&gt;. This comes in handy when your &lt;mod&gt;Range&lt;/mod&gt; spans multiple columns. For example,&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B:E&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;&#39;filters the third column, Column D&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B:E&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;&#39;filters the first column, Column B&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When the &lt;strong&gt;Range&lt;/strong&gt; object consists of only one column, such as &lt;code&gt;Columns(3).AutoFilter&lt;/code&gt;, the &lt;code&gt;Field&lt;/code&gt; parameter is still required - you’d just set it to 1.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;definingcriteria&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Defining the Filter Criteria&lt;/h2&gt;
&lt;p&gt;Filters are based on two &lt;code&gt;Criteria&lt;/code&gt; parameters and a set of special Excel operators, for a total of 3 parameters. Only a single criteria is required to filter, but all three can be used to filter a single column.&lt;/p&gt;

&lt;p&gt;You can practice using &lt;a href=&quot;/vba/assets/files/vba-filter-column-with-autofilter.csv&quot;&gt;this CSV&lt;/a&gt;, which contains this dataset:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-09-03-vba-filter-column-with-autofilter-dataset.png&quot; alt=&quot;Excel Dataset for Filtering Columns&quot; title=&quot;Excel Dataset for Filtering Columns&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;onestring&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Filter For a String&lt;/h3&gt;
&lt;p&gt;Let’s say you want to find all orders from the &lt;mod&gt;North&lt;/mod&gt; region. We can filter the &lt;mod&gt;Region&lt;/mod&gt; column like this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;E:E&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;North&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;which yields&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-09-03-vba-filter-column-with-autofilter-dataset-filtered-north.png&quot; alt=&quot;VBA AutoFilter with String Criteria&quot; title=&quot;VBA AutoFilter with String Criteria&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Note the filter is applied only to Column &lt;mod&gt;E&lt;/mod&gt;, as indicated by the filter icon at the top of column &lt;mod&gt;E&lt;/mod&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;multiplestrings&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Filter For Multiple Strings&lt;/h3&gt;
&lt;p&gt;What if we wanted all data from the North &lt;em&gt;and&lt;/em&gt; the West regions? This is where &lt;code&gt;Criteria2&lt;/code&gt; and &lt;code&gt;Operator&lt;/code&gt; comes in. The &lt;code&gt;Operator&lt;/code&gt; parameter accepts, among others, &lt;code&gt;xlAnd&lt;/code&gt; and &lt;code&gt;xlOr&lt;/code&gt;. In mathematics terms, AND intersects and OR unions. In non-technical terms, AND requires both criteria to be true, while OR requires only one (but at least one).&lt;/p&gt;

&lt;p&gt;So, to filter for all &lt;mod&gt;North&lt;/mod&gt; and all &lt;mod&gt;West&lt;/mod&gt; records, we could use this code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;E:E&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;North&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;West&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlOr&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-09-03-vba-filter-column-with-autofilter-dataset-filtered-north-or-west.png&quot; alt=&quot;Filtered for North or West&quot; title=&quot;Filtered for North or West&quot; /&gt;&lt;br /&gt;&lt;b&gt;Filtered for North or West&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;You can even use wildcards (the &lt;code&gt;*&lt;/code&gt; symbol) in strings. For example, if you wanted to find all first names that started with F or G, you could use&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B:B&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;F*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;G*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlOr&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you wanted cities that started with a C and end with an S, you could use &lt;code&gt;xlAnd&lt;/code&gt; to force both conditions to be true, like we do in this example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;C:C&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;C*&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;*S&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlAnd&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This would yield cities like Columbus and Colorado Springs. Except not in this dataset, because Colorado was not yet in existence when the Declaration of Independence was signed, and this list is mostly signatories of that document.&lt;/p&gt;

&lt;p&gt;You may have noticed Sun Tzu (who didn’t live in Ohio or sign the Declaration of Independence) appearing 4 times with exactly the same data. Duplicates like this are common issues in data science, so we made a guide showing you &lt;a href=&quot;/vba/2021/excel/vba-filter-unique-values/&quot;&gt;how to filter duplicates&lt;/a&gt; (using unique records), too.&lt;/p&gt;

&lt;hr /&gt;

&lt;p style=&quot;margin-bottom:10px;&quot;&gt;&lt;span style=&quot;font-size:26px;color:#000000;&quot;&gt;Make powerful macros with our free VBA Developer Kit&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;padding-top:10px;&quot;&gt;&lt;p&gt;It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free &lt;b&gt;VBA Developer Kit&lt;/b&gt; and wrote the &lt;b&gt;Big Book of Excel VBA Macros&lt;/b&gt; full of hundreds of pre-built macros to help you master file I/O, arrays, strings and more - grab your free copy below.&lt;/p&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;centered&quot;&gt;
&lt;form action=&quot;//wellsr.us13.list-manage.com/subscribe/post?u=9fa014ad47345eb4b70403d38&amp;amp;id=5c804a55ec&quot; method=&quot;post&quot; class=&quot;form-inline validate&quot; target=&quot;_blank&quot; id=&quot;email-subscribe&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-md-10 col-md-offset-1&quot;&gt;&lt;input required=&quot;&quot; type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;consent&quot; id=&quot;mce-Checkbox&quot; /&gt; I&#39;ll take a free VBA Developer Kit&lt;br /&gt;&lt;/div&gt;
&lt;input type=&quot;email&quot; value=&quot;&quot; name=&quot;EMAIL&quot; class=&quot;form-control email&quot; id=&quot;mce-EMAIL&quot; placeholder=&quot;Enter your email address&quot; required=&quot;&quot; /&gt;
&lt;div class=&quot;mc-field-group input-group&quot; style=&quot;display:none&quot;&gt;
    &lt;strong&gt;Programming Language &lt;/strong&gt;
    &lt;ul&gt;&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;1&quot; name=&quot;group[17657][1]&quot; id=&quot;mce-group[17657]-17657-0&quot; checked=&quot;&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-0&quot;&gt;VBA&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;group[17657][2]&quot; id=&quot;mce-group[17657]-17657-1&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-1&quot;&gt;Python&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups--&gt;
&lt;div style=&quot;position: absolute; left: -5000px;&quot; aria-hidden=&quot;true&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;b_9fa014ad47345eb4b70403d38_5c804a55ec&quot; tabindex=&quot;-1&quot; value=&quot;&quot; /&gt;&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-success btn-lg&quot; name=&quot;subscribe&quot; id=&quot;mc-embedded-subscribe&quot;&gt;Get my free kit&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;!--End mc_embed_signup--&gt;
&lt;/div&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;values&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Filter For Values&lt;/h3&gt;
&lt;p&gt;Business revolves around numbers, so surely we can filter on more than just strings? That’s right, we can also filter on values, like the &lt;mod&gt;Amount&lt;/mod&gt; values in Column &lt;mod&gt;G&lt;/mod&gt;.&lt;/p&gt;

&lt;p&gt;To find all orders over 500 - perhaps for expedited shipping - use the &lt;code&gt;Criteria1&lt;/code&gt; parameter and set it equal to &lt;code&gt;&quot;&amp;gt;500&quot;&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;G:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;gt;500&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;Criteria2&lt;/code&gt; and &lt;code&gt;Operator&lt;/code&gt; pramaters are not necessary since we are filtering the &lt;mod&gt;Amount&lt;/mod&gt; column on only one criterion.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;specialfilters&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Special Filters&lt;/h3&gt;
&lt;p&gt;We’ve talked about &lt;code&gt;xlAnd&lt;/code&gt; and &lt;code&gt;xlOr&lt;/code&gt; but the &lt;code&gt;Operator&lt;/code&gt; parameter also enables some special filters, like &lt;code&gt;xlTop10Percent&lt;/code&gt;. This will display the top 10% of the values in a field, so in a dataset of 50, you’ll get 5 and in a dataset of 1500, you’ll get 150.&lt;/p&gt;

&lt;p&gt;Although the name of the operator is &lt;code&gt;xlTop10Percent&lt;/code&gt;, you can adjust the actual percentage using &lt;code&gt;Criteria1&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;c1&quot;&gt;&#39;filter on Amount column (G)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;G:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlTop10Percent&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;&#39;top 5 percent&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;G:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlTop10Percent&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;&#39;top 25 percent&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;Operator&lt;/code&gt; parameter also has an &lt;code&gt;xlTop10Items&lt;/code&gt; option that restricts the response to X number of items, where X is defined using the &lt;code&gt;Criteria1&lt;/code&gt; parameter.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;c1&quot;&gt;&#39;filter on Amount column (G)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;G:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlTop10Items&lt;/span&gt;          &lt;span class=&quot;c1&quot;&gt;&#39;top 5 items&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;G:G&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Operator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlTop10Items&lt;/span&gt;         &lt;span class=&quot;c1&quot;&gt;&#39;top 10 items&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;userinput&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Leverage User Input&lt;/h3&gt;
&lt;p&gt;One neat thing about filtering is your filters don’t need to be hardwired. You can ask the user for input since the &lt;code&gt;Criteria&lt;/code&gt; parameters accept variables. This example simply asks for a region name in a message box, but with a bit of work on &lt;a href=&quot;/vba/vba-cheat-sheets/userforms/&quot;&gt;userforms&lt;/a&gt; you could design a nice dashboard with many business-specific options and constraints.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;filterOnRegion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;regionName&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;regionName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;InputBox&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Enter the Region name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;E:E&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Criteria1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;regionName&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;clearingfilters&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Clearing Filters&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Filters cannot be applied independently using &lt;code&gt;.AutoFilter&lt;/code&gt;.&lt;/strong&gt; Multiple criteria &lt;em&gt;can&lt;/em&gt; be applied and on different columns, but each new line of AutoFilter intersects the results - that is, each new line will only “see” the results of the previous filter. When you’re testing, this may cause runtime errors and plenty of frustration. Keep this in your &lt;a href=&quot;/vba/2018/excel/vba-immediate-window/&quot;&gt;Immediate Window&lt;/a&gt; (shortcut Ctrl+G) to quickly reset the filters while testing your code:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;ActiveSheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AutoFilterMode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;False&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you need multiple filters, check out our guide on &lt;a href=&quot;/vba/2018/excel/excel-vba-autofilter-to-filter-data-table/#Multiple&quot;&gt;how to properly apply multiple filters&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;As you can see, filtering a column in VBA is pretty easy, and you can even add some additional criteria without issue. You can filter on both numeric amounts and strings, and you can apply mathematical relations (like &lt;em&gt;greater than&lt;/em&gt;). Excel also provides some built-in dynamic filtering, like &lt;code&gt;xlTop10Percent&lt;/code&gt;, depending on your dataset&lt;/p&gt;

&lt;p&gt;Just keep in mind that AutoFilter only applies to one column at a time. You can filter multiple columns using it, but it requires multiple lines of code. For more VBA tutorials like this, subscribe using the form below.&lt;/p&gt;

</description>
        <pubDate>Fri, 17 Sep 2021 07:15:00 +0000</pubDate>
        <media:content url="https://wellsr.com/vba/assets/images/2021-09-17-vba-filter-column.png" medium="image"></media:content>
        <link>https://wellsr.com/vba/2021/excel/vba-filter-column-with-autofilter/</link>
        <guid isPermaLink="true">https://wellsr.com/vba/2021/excel/vba-filter-column-with-autofilter/</guid>
        
        
        <category>excel</category>
        
      </item>
    
      <item>
        <title>VBA Filter Unique Values with AdvancedFilter</title>
        <description>&lt;p&gt;When working with large datasets, there’s a good chance that one day you’ll need to find unique values, especially unique strings.  It might not matter how many times a value or string repeats, only that it exists in the dataset. When you have 50,000 records with potentially hundreds of unique strings, data cleansing becomes easier when these unique records are collected in one place.&lt;/p&gt;

&lt;p&gt;The VBA &lt;code&gt;.AdvancedFilter&lt;/code&gt; method is a very powerful tool for this. The method can retain old data, take worksheet-based criteria, and, most importantly for this tutorial, find unique values. The criteria portion itself is a powerful feature, but we’ll dive into that in another tutorial. Today, we’re going to show you how to collect unique values in one place.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#ranges&quot;&gt;Setting the Range &lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#multicolumn&quot;&gt;Unique Values Across Multiple Columns&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#copyornot&quot;&gt;Output in the Same Place or a New One? &lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#actionparam&quot;&gt;The &lt;code&gt;Action&lt;/code&gt; Parameter &lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#copytorange&quot;&gt;&lt;code&gt;CopyToRange&lt;/code&gt; Parameter &lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#findinguniques&quot;&gt;Finding Unique Values &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#originaluniques&quot;&gt;Is the Original Unique? Full Code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before we show you fully functional code, we’re going to step through the parameters of &lt;code&gt;AdvancedFilter&lt;/code&gt; so you can make sure you’re using it correctly.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;ranges&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Setting the Range&lt;/h2&gt;
&lt;p&gt;The AdvancedFilter method operates on a &lt;strong&gt;Range&lt;/strong&gt; object, just as any filter would. &lt;a href=&quot;/vba/excel/vba-range-object/&quot;&gt;Set the range normally&lt;/a&gt;, but note that VBA always treats the first row as a row containing headers. If your data has no headers - that is, your first cell is a regular value - the first value may appear twice in the uniques list. This is easy to recognize and adjust, but it is a bit inconvenient if you’re not expecting it. Unfortunately, Microsoft does not give us a &lt;code&gt;[SourceHasHeaders]&lt;/code&gt; type optional parameter here.&lt;/p&gt;

&lt;p&gt;Often we want to find uniques in just one column. If the values we want to filter are in Column C, then we’d operate on C like so&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;C:C&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt;     &lt;span class=&quot;c1&quot;&gt;&#39;Option 1 - using a general Range object&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt;       &lt;span class=&quot;c1&quot;&gt;&#39;Option 2 - using a column Range object&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is just the non-functional base code - we’ll show you how to get it working properly soon. Also, notice the range can be a single Column from the Columns collection, or it can be a range object. &lt;code&gt;.AdvancedFilter&lt;/code&gt; can operate on more than one column and its row range can be limited, as well, if you only want to filter a subset of the data.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;multicolumn&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Unique Values Across Multiple Columns&lt;/h3&gt;
&lt;p&gt;Filtering for unique values behaves properly across columns, too. For example, if Column A contains first names and Column B contains last names, we could use &lt;code&gt;.AdvancedFilter&lt;/code&gt; to find unique First Name + Last Name combinations. This can be extended to any number of columns. Just use &lt;code&gt;Range(&quot;A:B&quot;).AdvancedFilter&lt;/code&gt; to expand the filter target to both columns.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;copyornot&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Output in the Same Place or a New One?&lt;/h2&gt;
&lt;p&gt;VBA &lt;code&gt;AdvancedFilter&lt;/code&gt; can either filter in place, which hides any records that do not match the criteria, or it can output the results to a new location. While filtering in place has its uses, I recommend copying to a new location whenever you have columns on which you do not filter. This is good way to preserve your original data integrity.&lt;/p&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;actionparam&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;The &lt;code&gt;Action&lt;/code&gt; Parameter&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;Action&lt;/code&gt; parameter tells &lt;code&gt;.AdvancedFilter&lt;/code&gt; to&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;output results in the same place as the original (&lt;code&gt;xlFilterInPlace&lt;/code&gt;) or&lt;/li&gt;
  &lt;li&gt;copy results to a new location (&lt;code&gt;xlFilterCopy&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Beware of &lt;code&gt;xlFilterInPlace&lt;/code&gt;!&lt;/strong&gt; This will filter the entire sheet and hide rows that do not match your filtering condition on your specified column. This can cause users to panic because they don’t realize the data still exists. All you need to do to unhide the filtered rows is select the “Clear” button on the Excel Data tab to reshow the hidden rows. Alternatively, you can run this code to do it programmatically:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;ActiveSheet&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ShowAllData&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;copytorange&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;&lt;code&gt;CopyToRange&lt;/code&gt; Parameter&lt;/h3&gt;
&lt;p&gt;If you choose &lt;code&gt;xlFilterCopy&lt;/code&gt; as your &lt;code&gt;Action&lt;/code&gt; parameter, you need to specify where you want to place the copy. The &lt;code&gt;CopyToRange&lt;/code&gt; parameter lets you do this. All you need to do is specify a single cell or you can specify an entire column. If the output range is too small to contain all the results, VBA just overflows the area. This means you can’t limit the output, so choose a column without values or with values you don’t mind overwriting.&lt;/p&gt;

&lt;p&gt;There’s another quirk of &lt;code&gt;xlFilterCopy&lt;/code&gt; to be aware of, too. If two columns in the data you’re trying to filter have the same header title, the &lt;code&gt;xlFilterCopy&lt;/code&gt; feature may copy the first column with that name twice to your destination column (&lt;code&gt;CopyToRange&lt;/code&gt;).&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;findinguniques&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Finding Unique Values&lt;/h2&gt;
&lt;p&gt;The last piece of the puzzle is the Boolean parameter &lt;code&gt;Unique&lt;/code&gt;, which takes only TRUE or FALSE. To find unique values, set this to TRUE.&lt;/p&gt;

&lt;p&gt;To find each unique &lt;mod&gt;City&lt;/mod&gt; in the dataset shown below and place the list of unique cities in Column H, we can use this code&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;C:C&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xlFilterCopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;H1:H1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;True&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Don’t miss the blank &lt;code&gt;CriteriaRange&lt;/code&gt; parameter. This is our output:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-08-06-vba-filter-unique-values-unique-cities.PNG&quot; alt=&quot;VBA AdvancedFilter Unique Values from Column A to H&quot; title=&quot;VBA AdvancedFilter Unique Values from Column A to H&quot; /&gt;&lt;br /&gt;&lt;b&gt;Original Data in Column A and the Filtered Data in Column H&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Once you have the list of unique cities, you can further process your data if you’d like using &lt;a href=&quot;/vba/2018/excel/excel-vba-autofilter-to-filter-data-table/&quot;&gt;VBA AutoFilter&lt;/a&gt; to alphabetize or filter further.&lt;/p&gt;

&lt;hr /&gt;

&lt;p style=&quot;margin-bottom:10px;&quot;&gt;&lt;span style=&quot;font-size:26px;color:#000000;&quot;&gt;Make powerful macros with our free VBA Developer Kit&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;padding-top:10px;&quot;&gt;&lt;p&gt;It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free &lt;b&gt;VBA Developer Kit&lt;/b&gt; and wrote the &lt;b&gt;Big Book of Excel VBA Macros&lt;/b&gt; full of hundreds of pre-built macros to help you master file I/O, arrays, strings and more - grab your free copy below.&lt;/p&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;centered&quot;&gt;
&lt;form action=&quot;//wellsr.us13.list-manage.com/subscribe/post?u=9fa014ad47345eb4b70403d38&amp;amp;id=5c804a55ec&quot; method=&quot;post&quot; class=&quot;form-inline validate&quot; target=&quot;_blank&quot; id=&quot;email-subscribe&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-md-10 col-md-offset-1&quot;&gt;&lt;input required=&quot;&quot; type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;consent&quot; id=&quot;mce-Checkbox&quot; /&gt; I&#39;ll take a free VBA Developer Kit&lt;br /&gt;&lt;/div&gt;
&lt;input type=&quot;email&quot; value=&quot;&quot; name=&quot;EMAIL&quot; class=&quot;form-control email&quot; id=&quot;mce-EMAIL&quot; placeholder=&quot;Enter your email address&quot; required=&quot;&quot; /&gt;
&lt;div class=&quot;mc-field-group input-group&quot; style=&quot;display:none&quot;&gt;
    &lt;strong&gt;Programming Language &lt;/strong&gt;
    &lt;ul&gt;&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;1&quot; name=&quot;group[17657][1]&quot; id=&quot;mce-group[17657]-17657-0&quot; checked=&quot;&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-0&quot;&gt;VBA&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;group[17657][2]&quot; id=&quot;mce-group[17657]-17657-1&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-1&quot;&gt;Python&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups--&gt;
&lt;div style=&quot;position: absolute; left: -5000px;&quot; aria-hidden=&quot;true&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;b_9fa014ad47345eb4b70403d38_5c804a55ec&quot; tabindex=&quot;-1&quot; value=&quot;&quot; /&gt;&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-success btn-lg&quot; name=&quot;subscribe&quot; id=&quot;mc-embedded-subscribe&quot;&gt;Get my free kit&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;!--End mc_embed_signup--&gt;
&lt;/div&gt;
&lt;hr /&gt;

&lt;p&gt;To find unique given-family name combinations, you could use&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:B&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xlFilterCopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;H1:H1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;True&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Since our list is very short, there are no first-last name combination duplicates and the output matches the input. On the other hand, if we had 3 Samuel Adams, one each in Annapolis, New York, and Erie, we would only see a single instance in the output. Of course, we could also filter on &lt;mod&gt;Family Name&lt;/mod&gt; + &lt;mod&gt;Given Name&lt;/mod&gt; + &lt;mod&gt;City&lt;/mod&gt;, which would preserve all three in that example.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;originaluniques&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Is the Original Unique? Full Code&lt;/h2&gt;
&lt;p&gt;You can check if your original data has any duplicates by counting the input and output from the AdvancedFilter method. If the number of values match, then your original data didn’t have any duplicates to begin with. One way to do this is to use the &lt;code&gt;WorksheetFunction.Count&lt;/code&gt; method. The &lt;code&gt;xlFilterInPlace&lt;/code&gt; approach can also apply here, but keep in mind the warning we talked about earlier.&lt;/p&gt;

&lt;p&gt;This fully functional code block tells you whether the data in Column A is unique already or not:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;wasOriginalUnique&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;beforeCount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;afterCount&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:A&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xlFilterCopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B:B&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;True&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;beforeCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WorksheetFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CountA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:A&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;afterCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WorksheetFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CountA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;B:B&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;beforeCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;afterCount&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MsgBox&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;The original was unique&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;beforeCount&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;afterCount&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MsgBox&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;The original had repeated records&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To reinforce the value of the VBA &lt;code&gt;AdvancedFilter&lt;/code&gt; method, let’s so you have values in Column A and you want to copy only unique values to a new column, column H. It only takes one line of code to do this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;uniquesToColumnH&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A:A&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AdvancedFilter&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xlFilterCopy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;H:H&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;True&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;All your unique values from column A now appear in column H. It’s as simple as that. Advanced filtering unique values provides a valuable alternative to the &lt;a href=&quot;/vba/2017/excel/use-vba-removeduplicates-to-remove-duplicate-rows-from-column/&quot;&gt;VBA RemoveDuplicates&lt;/a&gt; method we wrote about not too long ago.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;We’ve now shown you how to filter out unique records, both in single columns or for consecutive columns. You also know how to put the results in a separate space for later comparison and to keep all the data on one page (without hiding large portions of your original dataset).&lt;/p&gt;

&lt;p&gt;Once you have the unique records, you can alphabetize and filter further using AutoFilter, both as an entire table or as a single column.&lt;/p&gt;

&lt;p&gt;If you found this helpful, please subscribe using the form below and we’ll send you a couple more tips to make sure you’re getting the most out of VBA.&lt;/p&gt;

</description>
        <pubDate>Fri, 06 Aug 2021 10:00:00 +0000</pubDate>
        <media:content url="https://wellsr.com/vba/assets/images/2021-08-06-vba-filter-unique-values.png" medium="image"></media:content>
        <link>https://wellsr.com/vba/2021/excel/vba-filter-unique-values/</link>
        <guid isPermaLink="true">https://wellsr.com/vba/2021/excel/vba-filter-unique-values/</guid>
        
        
        <category>excel</category>
        
      </item>
    
      <item>
        <title>VBA Insert Rows on Worksheets and Tables</title>
        <description>&lt;p&gt;Inserting a row in a spreadsheet seems like a straightforward task. If you don’t overthink it, you could probably guess right now how to &lt;code&gt;.Insert&lt;/code&gt; a &lt;strong&gt;Row&lt;/strong&gt; into the &lt;strong&gt;Rows&lt;/strong&gt; collection without looking up anything else. However, one of my favorite Bertrand Russell quotes applies here: everything is vague to a degree you do not realize until you have tried to make it precise.&lt;/p&gt;

&lt;p&gt;In that spirit, what exactly is meant by row? Is it the row of a table? The row of the entire sheet? Can a single cell be a row if we constrain ourselves to a single column? Let’s take a look at the different ways to insert rows.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#guessablemethod&quot;&gt;The Guessable Method &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#tablerow&quot;&gt;The Table Row &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#theshift&quot;&gt;The Shift &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#entirerowproperty&quot;&gt;The Entire Row Property &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#conclusion&quot;&gt;Conclusion &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;guessablemethod&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Guessable Method&lt;/h2&gt;
&lt;p&gt;I’m going to call this method the guessable method, because, well, even non-programmers could probably guess how it do it. Let’s say you want a new Row 2. Perhaps you need to load some data into it because you’re using a worksheet as a reverse chronological database for sales - against all advice, might I add. &lt;em&gt;Tip: don’t use Excel as a database. VBA works in Access, too.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To add a new Row 2, it really is as simple as&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Insert&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To be more precise, this accesses the &lt;strong&gt;Rows&lt;/strong&gt; collection and uses the &lt;code&gt;.Insert&lt;/code&gt; method for the second entry. &lt;code&gt;.Insert&lt;/code&gt; pertains to many objects, but VBA interpolates you would like to insert a row when accessing the &lt;strong&gt;Rows&lt;/strong&gt; collection.&lt;/p&gt;

&lt;p&gt;If you’d like to inject several rows, you need to put the range into quotes:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;2:4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Insert&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;&#39;this inserts THREE rows (2, 3, and 4)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This method inserts entire rows, from Column A all the way to Column XFD or whatever your rightmost column is. While this is the guessable method because it’s simple, simplicity is not the only method or even the preferred method for all use cases.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;tablerow&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Table Row&lt;/h2&gt;
&lt;p&gt;In spreadsheet terminology, “row” can be, believe it or not, a somewhat nebulous term. While &lt;code&gt;Rows(1).Insert&lt;/code&gt; does indeed insert a row, this type of row spans the entire worksheet. This method isn’t all that useful if you only want to change part of a table or if you have two tables side-by-side and you only want to target one of them.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-07-09-vba-insert-row-table.PNG&quot; alt=&quot;Small Excel table&quot; title=&quot;Small Excel table&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Let’s say you use a UserForm to gather data about orders, then have two subroutines that run: one injects new orders into the left table, while the other changes the inventory table on the right. &lt;code&gt;Rows.Insert&lt;/code&gt; will not produce the desired result here because you don’t want to shift rows in the inventory table on the right while updating the order table on the left.&lt;/p&gt;

&lt;p&gt;To target a row within a table, you can target the top row of the table using the range in two ways. One way is specify the columns and rows in your statement:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;InsertRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A2:E2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Insert&lt;/span&gt;      &lt;span class=&quot;c1&quot;&gt;&#39;target the row directly&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In fact, you could even drop the &lt;code&gt;.Rows&lt;/code&gt; part from this and get the same result, because Excel understands this range is indeed a single row.&lt;/p&gt;

&lt;p&gt;Alternatively, if you prefer to use relative locations:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;InsertRow2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A1:E2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Rows&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Insert&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;&#39;target the second row in the range A1:E2&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The second method is useful if you already have a named range for the table or have a looping process. This version does indeed require the &lt;code&gt;.Rows&lt;/code&gt; part to specify which part of the range to change. Otherwise, Excel wouldn’t know whether you wanted to insert rows or columns.&lt;/p&gt;

&lt;p&gt;With all the macro examples in this section, we’re not inserting an entire row across your whole spreadsheet, like we did in our first example. We’re simply inserting cells in a row within a defined range.&lt;/p&gt;

&lt;hr /&gt;

&lt;p style=&quot;margin-bottom:10px;&quot;&gt;&lt;span style=&quot;font-size:26px;color:#000000;&quot;&gt;Make powerful macros with our free VBA Developer Kit&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;padding-top:10px;&quot;&gt;&lt;p&gt;It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free &lt;b&gt;VBA Developer Kit&lt;/b&gt; and wrote the &lt;b&gt;Big Book of Excel VBA Macros&lt;/b&gt; full of hundreds of pre-built macros to help you master file I/O, arrays, strings and more - grab your free copy below.&lt;/p&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;centered&quot;&gt;
&lt;form action=&quot;//wellsr.us13.list-manage.com/subscribe/post?u=9fa014ad47345eb4b70403d38&amp;amp;id=5c804a55ec&quot; method=&quot;post&quot; class=&quot;form-inline validate&quot; target=&quot;_blank&quot; id=&quot;email-subscribe&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-md-10 col-md-offset-1&quot;&gt;&lt;input required=&quot;&quot; type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;consent&quot; id=&quot;mce-Checkbox&quot; /&gt; I&#39;ll take a free VBA Developer Kit&lt;br /&gt;&lt;/div&gt;
&lt;input type=&quot;email&quot; value=&quot;&quot; name=&quot;EMAIL&quot; class=&quot;form-control email&quot; id=&quot;mce-EMAIL&quot; placeholder=&quot;Enter your email address&quot; required=&quot;&quot; /&gt;
&lt;div class=&quot;mc-field-group input-group&quot; style=&quot;display:none&quot;&gt;
    &lt;strong&gt;Programming Language &lt;/strong&gt;
    &lt;ul&gt;&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;1&quot; name=&quot;group[17657][1]&quot; id=&quot;mce-group[17657]-17657-0&quot; checked=&quot;&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-0&quot;&gt;VBA&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;group[17657][2]&quot; id=&quot;mce-group[17657]-17657-1&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-1&quot;&gt;Python&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups--&gt;
&lt;div style=&quot;position: absolute; left: -5000px;&quot; aria-hidden=&quot;true&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;b_9fa014ad47345eb4b70403d38_5c804a55ec&quot; tabindex=&quot;-1&quot; value=&quot;&quot; /&gt;&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-success btn-lg&quot; name=&quot;subscribe&quot; id=&quot;mc-embedded-subscribe&quot;&gt;Get my free kit&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;!--End mc_embed_signup--&gt;
&lt;/div&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;theshift&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Shift&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;.Insert&lt;/code&gt; method takes two optional inputs: &lt;code&gt;Shift&lt;/code&gt; and &lt;code&gt;CopyOrigin&lt;/code&gt;. Since they’re optional you don’t need to use them, but they can be useful in some instances.&lt;/p&gt;

&lt;p&gt;If you’re targeting a range, such as the first row of the Inventory table, normally you would shift the cells down. However, you can also shift the targeted cells to the right. This is precisely why Excel asks this question:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-07-09-vba-insert-row-shiftdirection.PNG&quot; alt=&quot;Excel GUI Shift Down or Right&quot; title=&quot;Excel GUI Prompt&quot; /&gt;&lt;br /&gt;&lt;b&gt;Excel GUI Prompt for How to Shift after Insert Cells&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;In most cases, you’ll probably be using &lt;code&gt;xlShiftDown&lt;/code&gt;, which is the default for &lt;code&gt;.Insert&lt;/code&gt; when referring to rows. However, sometimes you might want to copy to the right, say for a visual check after the subroutine runs:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;H2:J2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Insert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;xlShiftToRight&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will produce the following result, where the range &lt;mod&gt;H2:J2&lt;/mod&gt; was inserted and the original values were moved to the right:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/vba/assets/images/2021-07-09-vba-insert-row-rightshifted.PNG&quot; alt=&quot;Cells Shifted to Right&quot; title=&quot;Cells Shifted to Right&quot; /&gt;&lt;br /&gt;&lt;b&gt;Cells Shifted to Right&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;The other optional parameter chooses where to copy the format of the inserted cells from. The default is &lt;code&gt;xlFormatFromLeftOrAbove&lt;/code&gt;. In our table example, if we insert at &lt;mod&gt;&quot;A2:E2&quot;&lt;/mod&gt;, we copy the cell formatting from &lt;mod&gt;&quot;A1:E1&quot;&lt;/mod&gt;. Since this is our header, the inserted cells are initially set as &lt;strong&gt;bolded font&lt;/strong&gt;. In order to ensure we have non-bold font, you should explicitly specify the &lt;code&gt;CopyOrigin&lt;/code&gt; parameter:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;A2:E2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Insert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CopyOrigin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;xlFormatFromRightOrBelow&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This ensures that the inserted cells use the format of the cells below the range before the insertion instead of what’s above (the header format).&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;entirerowproperty&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Entire Row Property&lt;/h2&gt;
&lt;p&gt;The Excel GUI prompt above also allows you to insert an &lt;code&gt;Entire Row&lt;/code&gt;. From our example earlier, this would affect the order table on the left and all other cells on the sheet, including the inventory table on the right. It’s equivalent to the Guessable Method, except the initial reference is a range of cells rather than the &lt;strong&gt;Rows&lt;/strong&gt; collection directly.&lt;/p&gt;

&lt;p&gt;To replicate the GUI’s &lt;code&gt;Insert Entire Row&lt;/code&gt; from this example, you’ll add the the &lt;code&gt;EntireRow&lt;/code&gt; property of the Range object:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;Range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;H2:J2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EntireRow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Insert&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is most useful if you only have a cell location (especially a relative location, so you don’t know the true location) and you want to insert a full row.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;conclusion&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Now you can insert rows across the entire sheet or only for specific ranges using VBA. In the latter case, you can now also choose whether to shift the cells down or to the right of the newly-inserted cells. And finally, you can choose the format of the new cells from the surrounding, existing cells.&lt;/p&gt;

&lt;p&gt;You also learned a little about the &lt;code&gt;EntireRow&lt;/code&gt; property, which can be useful in same instances, especially when programming complex macros where you don’t know the exact cell reference.&lt;/p&gt;

</description>
        <pubDate>Wed, 07 Jul 2021 10:00:00 +0000</pubDate>
        <media:content url="https://wellsr.com/vba/assets/images/2021-07-09-vba-insert-row.png" medium="image"></media:content>
        <link>https://wellsr.com/vba/2021/excel/vba-insert-row/</link>
        <guid isPermaLink="true">https://wellsr.com/vba/2021/excel/vba-insert-row/</guid>
        
        
        <category>excel</category>
        
      </item>
    
      <item>
        <title>How To Use VBA GetAttr To Gather File Information</title>
        <description>&lt;p&gt;Sometimes you may find it useful to know basic file attributes of the files or filepaths with which you’re interacting. If you read and then write back to a file, it’s important to know if the original is read-only, in which case your write will fail, or if it’s a system file, in which case the write will probably also fail but if it succeeds, you could damage the system. Other times, you may just want to check that a string indeed points to a valid file or a directory.&lt;/p&gt;

&lt;p&gt;The VBA &lt;code&gt;GetAttr&lt;/code&gt; function will, unsurprisingly, return basic attributes of a file. Keep in mind, this is a function so it outputs a value; &lt;strong&gt;it is not a property of a file object!&lt;/strong&gt; You cannot write to &lt;code&gt;GetAttr&lt;/code&gt; to change the attributes of a file. Likewise, you can’t use this function to make files read-only or hide files.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GetAttr&lt;/code&gt; works in a rather clever way, so we’ll outline bitwise operations to help our understanding. Don’t worry, though, you can skip that section if you prefer.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;#attributes&quot;&gt;The Possible Attributes &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#output&quot;&gt;The Output &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#unmixing&quot;&gt;Unmixing Attributes &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#decomposing&quot;&gt;Decomposing Bitwise AND &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;attributes&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Possible Attributes&lt;/h2&gt;
&lt;p&gt;There are 7 possible attributes and more than one may be true for a given filepath, though there are some mutually exclusive ones.&lt;/p&gt;

&lt;p&gt;Here are the attributes, straight from the &lt;a href=&quot;https://docs.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/getattr-function&quot;&gt;Microsoft documentation&lt;/a&gt; (literally - this is their table):&lt;/p&gt;

&lt;table class=&quot;table table-striped table-bordered&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style=&quot;text-align: left;&quot;&gt;Constant&lt;/th&gt;
&lt;th style=&quot;text-align: left;&quot;&gt;Value&lt;/th&gt;
&lt;th style=&quot;text-align: left;&quot;&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;vbNormal&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;0&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Normal.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;vbReadOnly&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;1&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Read-only.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;vbHidden&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;2&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Hidden.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;vbSystem&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;4&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;System file. Not available on the Macintosh.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;vbDirectory&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;16&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Directory or folder.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;vbArchive&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;32&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;File has changed since last backup. Not available on the Macintosh.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;&lt;strong&gt;vbAlias&lt;/strong&gt;&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;64&lt;/td&gt;
&lt;td style=&quot;text-align: left;&quot;&gt;Specified file name is an alias. Available only on the Macintosh.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The first column is the VBA name, which is a human-understandable name for each of the constants in the second column. In your macros, you can use the &lt;mod&gt;vbXX&lt;/mod&gt; name interchangeably with the number itself.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;output&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;The Output&lt;/h2&gt;
&lt;p&gt;So what exactly does the &lt;code&gt;GetAttr&lt;/code&gt; function output? The only output is a single &lt;strong&gt;integer&lt;/strong&gt; value equal to the sum of all the true attributes. Whichever attribute is true for your particular input will appear in the function output.&lt;/p&gt;

&lt;p&gt;The simplest case is one where the file satisfies only one attribute. Let’s say &lt;mod&gt;myFile&lt;/mod&gt; is hidden and this is the only attribute from the above list. Then the statement &lt;code&gt;GetAttr(myFile)&lt;/code&gt; will evaluate to 2.&lt;/p&gt;

&lt;p&gt;Slightly more complicated is a hidden system file. Here we have to include 4 and 2 in the sum, so the output is 6: &lt;code&gt;GetAttr(myFile)&lt;/code&gt; = vbHidden + vbSystem = 6.&lt;/p&gt;

&lt;p&gt;What’s the value for a hidden directory? vbHidden + vbDirectory = 2 + 16 = 18. A hidden read-only system file? vbReadOnly + vbHidden + vbSystem = 7.&lt;/p&gt;

&lt;p&gt;The reason the numbers are successive powers of 2 is twofold: every combination of attributes will always give a unique number and the mechanics of binary addition make bitwise operation easier.&lt;/p&gt;

&lt;p&gt;This clever technique means multiple attributes can be represented by a single number without losing any information, like multiple dimensions collapsing into one.&lt;/p&gt;

&lt;hr /&gt;

&lt;p style=&quot;margin-bottom:10px;&quot;&gt;&lt;span style=&quot;font-size:26px;color:#000000;&quot;&gt;Make powerful macros with our free VBA Developer Kit&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;padding-top:10px;&quot;&gt;&lt;p&gt;It’s easy to copy and paste a macro like this, but it’s harder make one on your own. To help you make macros like this, we built a free &lt;b&gt;VBA Developer Kit&lt;/b&gt; and wrote the &lt;b&gt;Big Book of Excel VBA Macros&lt;/b&gt; full of hundreds of pre-built macros to help you master file I/O, arrays, strings and more - grab your free copy below.&lt;/p&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;centered&quot;&gt;
&lt;form action=&quot;//wellsr.us13.list-manage.com/subscribe/post?u=9fa014ad47345eb4b70403d38&amp;amp;id=5c804a55ec&quot; method=&quot;post&quot; class=&quot;form-inline validate&quot; target=&quot;_blank&quot; id=&quot;email-subscribe&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-md-10 col-md-offset-1&quot;&gt;&lt;input required=&quot;&quot; type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;consent&quot; id=&quot;mce-Checkbox&quot; /&gt; I&#39;ll take a free VBA Developer Kit&lt;br /&gt;&lt;/div&gt;
&lt;input type=&quot;email&quot; value=&quot;&quot; name=&quot;EMAIL&quot; class=&quot;form-control email&quot; id=&quot;mce-EMAIL&quot; placeholder=&quot;Enter your email address&quot; required=&quot;&quot; /&gt;
&lt;div class=&quot;mc-field-group input-group&quot; style=&quot;display:none&quot;&gt;
    &lt;strong&gt;Programming Language &lt;/strong&gt;
    &lt;ul&gt;&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;1&quot; name=&quot;group[17657][1]&quot; id=&quot;mce-group[17657]-17657-0&quot; checked=&quot;&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-0&quot;&gt;VBA&lt;/label&gt;&lt;/li&gt;
&lt;li&gt;&lt;input type=&quot;checkbox&quot; value=&quot;2&quot; name=&quot;group[17657][2]&quot; id=&quot;mce-group[17657]-17657-1&quot; /&gt;&lt;label for=&quot;mce-group[17657]-17657-1&quot;&gt;Python&lt;/label&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups--&gt;
&lt;div style=&quot;position: absolute; left: -5000px;&quot; aria-hidden=&quot;true&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;b_9fa014ad47345eb4b70403d38_5c804a55ec&quot; tabindex=&quot;-1&quot; value=&quot;&quot; /&gt;&lt;/div&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-success btn-lg&quot; name=&quot;subscribe&quot; id=&quot;mc-embedded-subscribe&quot;&gt;Get my free kit&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;!--End mc_embed_signup--&gt;
&lt;/div&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;unmixing&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Unmixing Attributes&lt;/h2&gt;
&lt;p&gt;You could look at a table of all of the possible outputs and know which attributes are present. However, most of the time we are only interested in one attribute. Possible values for read-only inputs are 1, 3, 5, 35, and others. But it would be cumbersome to test each of these. To make it easier, we can rely on the cleverness of binary bitwise operations.&lt;/p&gt;

&lt;p&gt;To see whether a particular attribute is present, you’ll need to use the AND operator and set the resulting value to an integer:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;n&quot;&gt;iReadOnly&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;And&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vbReadOnly&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If the output is zero, &lt;code&gt;vbReadOnly&lt;/code&gt; is not an attribute of this file. If the output is &lt;em&gt;anything other than zero&lt;/em&gt;, &lt;code&gt;vbReadOnly&lt;/code&gt; is present. Here’s a full macro example to test the read-only attribute using a VBA If-Then statement:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-vb&quot; data-lang=&quot;vb&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;VBA_GetAttr_Demo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;myFile&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;iReadOnly&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;myFile&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;C:\Users\Public\MySpreadsheet.xlsm&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;iReadOnly&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;And&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vbReadOnly&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iReadOnly&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;&#39;File is read-only&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Else&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;&#39;File is not read-only&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;If&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To drive the message home, let’s run through a second example to test whether a a path is a directory or a file:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;VBA_GetAttr_Demo_Dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;myPath&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Dim&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;iDir&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;As&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;myPath&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;C:\Users\Public&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;iDir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetAttr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;myPath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;And&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vbDirectory&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;If&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iDir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Then&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;&#39;A directory was selected&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;Else&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;&#39;Not a directory&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;If&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;End&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Sub&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This will work whether or not you have trailing slash at the end of your file path.&lt;/p&gt;

&lt;p&gt;Testing is as easy as that. To use &lt;code&gt;GetAttr&lt;/code&gt; you don’t need any more information, but if you’re curious how we can break down the sum, read the next section.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;a class=&quot;anchor&quot; id=&quot;decomposing&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Decomposing Bitwise AND&lt;/h2&gt;
&lt;p&gt;So how can we test whether a number is really part of the sum? By using bitwise &lt;code&gt;AND&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Every number in computing is represented by a string of bits, which can be on/true or off/false, usually represented as 1 and 0, respectively. There are two operations we can do with these bits, &lt;code&gt;AND&lt;/code&gt; and &lt;code&gt;OR&lt;/code&gt;, which come from mathematical logic. For this article, the important point is that both sides of an AND operation must be true/on/1 for the output to be &lt;mod&gt;1&lt;/mod&gt;. If one or both sides is false/off/0, then the output is &lt;mod&gt;0&lt;/mod&gt;.&lt;/p&gt;

&lt;p&gt;When we look at the bitstring representation of 2, we get &lt;code&gt;10&lt;/code&gt;. Leading zeros can be added indefinitely, so &lt;code&gt;10&lt;/code&gt; = &lt;code&gt;00000010&lt;/code&gt; = &lt;code&gt;0010&lt;/code&gt;. The important part is the trailing zeros (and 1s).&lt;/p&gt;

&lt;p&gt;The bitstring representation of 4 is &lt;code&gt;100&lt;/code&gt; and 8 is &lt;code&gt;1000&lt;/code&gt;. It is best to read these as “one zero zero” and “one zero zero zero” rather than “one hundred”, “one thousand”, etc. 6 is 2 + 4, or &lt;code&gt;110 = 010 + 100&lt;/code&gt;.  Here are the bitstrings of the attribute values above:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;mi&quot;&gt;00&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0000000&lt;/span&gt;
     &lt;span class=&quot;mi&quot;&gt;6543210&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;--&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;this&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;power&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;appears&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;01&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0000001&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;02&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0000010&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;04&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0000100&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;08&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0001000&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0010000&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0100000&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000000&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Because each value in the table above is a power of 2, all bits in the bitstring representation will be zero except one of them. For this reason, adding any of these numbers together will never “flip a bit” and carry over to the next column, as each number is fully represented in its own column.&lt;/p&gt;

&lt;p&gt;To see whether 4 is “part of” 6, we can check whether each 1-bit in 4 has a corresponding 1-bit in 6. Remember this is &lt;em&gt;bitwise&lt;/em&gt; operation, so we need to go column-by-column:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-visualbasic&quot; data-lang=&quot;visualbasic&quot;&gt;&lt;span class=&quot;mi&quot;&gt;06&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0000110&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;AND&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;04&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0000100&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;xx&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0000100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;--&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bitwise&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bits&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;top&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bottom&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;are&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BOTH&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the 3rd column from the right, which is four’s designated column, we have a 1-bit in 6. The bitwise AND operator output produces a non-zero number, and hence 4, vbSystem, is part of 6. If we have 14 (2 + 4 + 8), can you work out how the calculations look to determine whether vbArchive is present?&lt;/p&gt;

&lt;p&gt;This trick does &lt;em&gt;not&lt;/em&gt; work if the possible inputs for the sum are not powers of 2.&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;The VBA &lt;code&gt;GetAttr&lt;/code&gt; function provides information about the attributes of a file using a summation technique that provides a unique number for each combination of attributes. Again, the VBA &lt;code&gt;GetAttr&lt;/code&gt; function is a function, not a file property, so you can’t use it to change file attributes. You can only use it to determine which file attributes are present.&lt;/p&gt;

&lt;p&gt;Whether a particular attribute is present can be determined using the bitwise &lt;code&gt;AND&lt;/code&gt; operator. Bitwise operations go bit-by-bit rather than taking the bitstring as a whole, and the gaps in the decimal representation of the attributes make bitwise &lt;code&gt;AND&lt;/code&gt; able to detect whether an attribute is true or not.&lt;/p&gt;

&lt;p&gt;Bitwise operations can be complicated, but the GetAttr function, and VBA in general, don’t have to be. If you want to learn VBA without all the unnecessary coding complications, subscribe using the form below. We’ll walk you through a free step-by-step training program to making mastering VBA a breeze.&lt;/p&gt;

</description>
        <pubDate>Sat, 12 Jun 2021 07:00:00 +0000</pubDate>
        <media:content url="https://wellsr.com/vba/assets/images/2021-06-11-vba-getattr.png" medium="image"></media:content>
        <link>https://wellsr.com/vba/2021/excel/vba-getattr/</link>
        <guid isPermaLink="true">https://wellsr.com/vba/2021/excel/vba-getattr/</guid>
        
        <category>io</category>
        
        
        <category>excel</category>
        
      </item>
    
  </channel>
</rss>
