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

Compare Two Lists on Different Sheets and Output Matches

December 17, 2016 Marcus Small
Compare Lists VBA Scripting

 

There has been a number of articles written on thesmallman.com about matching information.  I tend to lean towards the scripting dictionary.  I had an email from a friend recently saying they were trying to adapt code from these two posts:

Compare Two Worksheets

Compare Two Lists with VBA

The idea was to compare two lists on different sheets and output the matches to a third sheet.  So I decided to combine the two techniques into one.  It was not a great deal of work.  The general premise is that the scripting dictionary removes the non matching items and all that is left are the items that are matching.  The current region is trapped however only the two lists are compared.  The upside is that when a match is made the entire row for each match is returned to sheet3.

Sub CompareSolve()
Dim i As Long
Dim j As Long
Dim n As Long
Dim ar As Variant

ar = Sheet2.Cells(1, 3).CurrentRegion.Value

With CreateObject("Scripting.Dictionary")
.CompareMode = 1
    For i = 2 To Ubound (ar, 1)
       .Item(ar(i, 1)) = Empty
    Next
   
ar = Sheet1.Cells(1).CurrentRegion.Value
n = 1    

For i = 2 To Ubound (ar, 1)
    If .exists(ar(i, 1)) Then
       n = n + 1
            For j = 1 To UBound(ar, 2)
                   ar(n, j) = ar(i, j)
           Next j
   End If
Next i
End With    
Sheet3.Cells(1).Resize(n, UBound(ar, 2)).Value = ar
End Sub

The following Excel file shows the procedure in a practical context. The Excel workbook will copy all of the like data between Col C of sheet2 and Col A of sheet1 and move the data to the Output sheet. Simples :)

MatchandOutput.xlsm

 

 

Tags Excel, Compare, two Lists
← Excel Dashboards Essential UpdatesCopy/Paste Top n Visible Rows →

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.