Excel

TheSmallman.com

an XL ideas Lab

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

Excel Dashboards VBA

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

Your Custom Text Here

Excel Dashboards VBA

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

Find Closest Match in Excel

February 6, 2018 Marcus Small
Photo by Devin Avery on Unsplash

Photo by Devin Avery on Unsplash

Finding the location of closest match in a given list is a task I was given recently.  The list was rather extensive and I needed to generate a nearest match and place the term “Close” in the adjacent column.  It is a tricker task than first anticipated as you need to keep a running total of the number closest to the target figure. The following VBA code will loop through a range of cells and evaluate the closest match.  

 

SubNearestFig()
Dim rng as Range
Dim r as Range
Dim Mx as Long
Dim i as Long
Const target as Integer = 100

Set rng = Range([B2], Range("B" & Rows.Count).End(xlUp))
rng.Offset(, 1).ClearContents
Mx = Application.Max(rng)

   For Each r In rng
      If Abs(target - r) < Mx Then
         Mx = Abs(target - r)
         i = r.Row
     End If
   Next r
Cells(i, 3) = "Close"
End Sub


The key to finding the closest value is to find the smallest number  between your target (the number you are aiming to match) and the number in the cell being evaluated.  If the difference between the two values is zero then you have your match, otherwise the closest number to zero in your range is the closest match as this number is the nearest.

For this particular file the numbers being evaluated are in column B.  Change to suit.  A file is attached below to show workings.

 

ClosestMatch.xlsm

Source: Photo by Hannah Joshua on Unsplash
← More Dashboard UpdatesDelete Blank and Zero Value Cells from Columns →

Featured Posts

Excel Dashboards: Tracking a Crisis

Excel Dashboards: Tracking a Crisis
April 14, 2020

Recent Posts

Populating an Excel Table from a Range of Cells with VBA

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

Fuzzy Distribution with Randbetween

Fuzzy Distribution with Randbetween May 21, 2025

Add Minimum and Maximum for Chart in Cells

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

Inflation Over Multiple Years in a Single Cell

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

Hubspot Dashboard

Hubspot Dashboard October 3, 2024

Monthly Dashboard With Supporting Metrics

Monthly Dashboard With Supporting Metrics September 25, 2024

Excel Show Missing Sheet Tabs

Excel Show Missing Sheet Tabs July 29, 2024

Run Macro Overnight Automatically

Run Macro Overnight Automatically June 24, 2024

Split File into Parts and Save to Directory

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

Most Popular Author

Most Popular Author December 14, 2023

 

Follow US:

 
 

MarcusSmall@thesmallman.com

 

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