Excel VBA - Compare Two Lists and Remove Duplicates

Computergaga
1 Feb 201707:41

Summary

TLDRThis tutorial from computergar.com demonstrates using Excel VBA to eliminate duplicates between two customer lists. The video showcases a practical example with highlighted duplicates for clarity. It guides viewers through the VBA code, explaining the process of identifying and removing matching items. The presenter also offers a downloadable file with the code and mentions an online Excel VBA course for further learning. The demonstration concludes with a macro execution that successfully removes the duplicates, leaving only unique customers in the list.

Takeaways

  • 😀 The video tutorial is from computerggar.com and focuses on using Excel VBA to compare two lists and remove duplicates.
  • 📚 The demonstration involves two worksheets within a workbook: 'customers one' and 'customers two'.
  • 🔍 The script explains that the duplicates are already highlighted in the demonstration for clarity, but this step is not necessary for the process to work.
  • 💡 The video provides a link in the description to download the VBA code used in the tutorial, allowing viewers to learn and adapt the code for their needs.
  • 🎓 A link to an online Excel VBA course is also provided for those who want to learn more about automating Excel procedures.
  • 📈 The tutorial begins by declaring variables for worksheets, row numbers, and tracking duplicates.
  • 🔄 The process involves looping through the first customer list and checking against the second list to find and remove duplicates.
  • 🔍 The script uses a 'do while' loop to iterate through the first list and a nested loop to search for matches in the second list.
  • 👷‍♂️ When a duplicate is found, the corresponding row in the first list is removed, and a count of duplicates is updated.
  • 📉 The script accounts for the shifting of row numbers when a row is deleted by adjusting the row number accordingly.
  • 📣 At the end of the process, a message box confirms that the duplicates have been removed, demonstrating the successful execution of the macro.

Q & A

  • What is the main purpose of the video tutorial?

    -The main purpose of the video tutorial is to demonstrate how to use Excel VBA to compare two lists and remove duplicates.

  • What are the two worksheets in the workbook used for?

    -The two worksheets, 'customers one' and 'customers two', are used for identifying and removing duplicate customer entries from 'customers two'.

  • Why are the duplicate customers highlighted in color in the demonstration?

    -The duplicate customers are highlighted in color to visually indicate what to expect from the video and what the ultimate result will be, but this is not a requirement for the VBA code to work.

  • What does the provided macro code do?

    -The macro code provided is designed to automate the process of finding and removing duplicate entries between the two customer lists in an Excel workbook.

  • How can viewers access the VBA code used in the tutorial?

    -The VBA code file is made available via a link in the description of the video, allowing viewers to download, learn from, and use the code.

  • What additional resource is mentioned in the video description for those who want to learn more about Excel VBA?

    -An online Excel VBA course is mentioned in the video description, which can be accessed for a fee of $49 for those who want to learn more about automating Excel procedures.

  • What are the key elements of the VBA code discussed in the tutorial?

    -The key elements of the VBA code include variable declarations, loops for iterating through the lists, conditional statements for finding matches, and a message box to indicate the completion of the duplicate removal process.

  • How does the VBA code handle the removal of duplicates?

    -The VBA code uses an external loop to iterate through the first list and an internal loop to search for each entry in the second list. If a duplicate is found, the corresponding row in the first list is removed, and a counter is adjusted to account for the change in row numbers.

  • What is the significance of adjusting the row number with a minus one operation after removing a duplicate?

    -The minus one operation ensures that after a row is deleted, the code does not skip the next row in the list, as the subsequent rows move up to fill the gap left by the deleted row.

  • How does the tutorial demonstrate the successful removal of duplicates?

    -The tutorial demonstrates the successful removal of duplicates by running the macro and showing that the highlighted entries in the 'customers one' list are no longer present after the macro execution.

Outlines

00:00

📊 Excel VBA Tutorial: Removing Duplicates

This video tutorial from computer ggar.com introduces viewers to the process of using Excel VBA to eliminate duplicate entries from a list. The demonstration involves two worksheets within a workbook, labeled 'customers one' and 'customers two.' The presenter has marked duplicate customers in 'customers one' for clarity. The video provides a step-by-step guide through the VBA code necessary for the task, which is available via a link in the video description. The code includes variable declarations for tracking row numbers and identifying duplicates. The main focus is on the looping structure, which iterates through the first list to find and remove duplicates in the second list. The video concludes with a demonstration of the macro in action, showing the removal of highlighted duplicates and a message box confirming the completion of the task.

05:01

🔍 Detailed Explanation of VBA Code for Duplicate Removal

In this paragraph, the presenter delves deeper into the VBA code used for removing duplicates from an Excel list. The explanation covers the initialization of variables, the purpose of each, and the logic behind the looping structure. The external loop iterates through the first customer list, while the internal loop searches for matches in the second list. When a duplicate is found, the corresponding row in the first list is removed, and a counter is adjusted to account for the change in row numbers due to deletion. The presenter emphasizes the importance of not moving to the next row after deletion to avoid skipping any entries. The video also mentions a message box that appears at the end to notify the user that duplicates have been removed. The presenter encourages viewers to follow the link in the description to access the file and use the provided code, suggesting further learning through an online Excel VBA course for those interested in automating procedures.

Mindmap

Keywords

💡Excel VBA

Excel VBA (Visual Basic for Applications) is a programming language used to automate tasks in Microsoft Excel. In the video, Excel VBA is used to compare two lists and remove duplicates, streamlining the process of managing customer data in Excel.

💡Compare two lists

Comparing two lists involves checking two sets of data for matching entries. In the video, the tutorial shows how to use VBA code to compare customer lists in two worksheets and identify duplicate entries that need to be removed from the first list.

💡Remove duplicates

Removing duplicates refers to eliminating repeated entries from a dataset. The video demonstrates how to use VBA code to identify and delete duplicate customer records in one Excel worksheet based on comparisons with another worksheet.

💡Worksheets

Worksheets are individual pages within an Excel workbook where data is stored and manipulated. The video uses two worksheets, 'customers 1' and 'customers 2,' to illustrate the process of comparing lists and removing duplicates using VBA code.

💡Macro

A macro is a set of instructions that can be triggered to perform a specific task automatically in Excel. The video features a macro designed to compare customer lists and remove duplicates, showcasing the practical application of VBA in automating repetitive tasks.

💡Loops

Loops are programming constructs that repeat a block of code multiple times. In the video, loops are used in the VBA code to iterate through customer lists and check for duplicates. The 'do while' loop and 'for' loop are specifically mentioned.

💡Variables

Variables are used in programming to store data that can be referenced and manipulated. The VBA code in the video declares several variables to track worksheets, row numbers, customer IDs, and the number of duplicates found, facilitating the comparison process.

💡Developer tab

The Developer tab in Excel provides access to advanced features like VBA and macros. In the video, the instructor accesses the Developer tab to open the Visual Basic for Applications editor, where the VBA code is written and executed.

💡Visual Basic for Applications editor

The VBA editor is a tool within Excel where users can write, edit, and run VBA code. The video shows the instructor using the VBA editor to explain and execute the code that compares customer lists and removes duplicates.

💡Message box

A message box is a dialog box in Excel VBA that displays information to the user. At the end of the VBA script in the video, a message box appears to notify the user that the duplicates have been removed, enhancing the script's interactivity.

Highlights

Introduction to using Excel VBA to compare two lists and remove duplicates.

Demonstration with two worksheets named 'customers one' and 'customers two'.

Color coding is used for demonstration purposes to identify duplicates.

Customers like Lawrence, Philipe, Jose, and Rita are marked as duplicates in 'customers one'.

Customers 2 list is larger and contains duplicates.

Macro code is provided to automate the duplicate removal process.

Link to the macro file and Excel VBA course will be provided in the video description.

Explanation of the initial variable declarations in the VBA code.

How the VBA code determines the number of customers in the second list.

Description of the external loop that iterates through the first customer list.

Explanation of the internal loop that searches for duplicates in the second list.

Process of identifying and removing duplicates when found.

Adjustment of row numbers after removing duplicates to continue the search.

Final message box indicating the completion of duplicate removal.

Demonstration of running the macro to remove duplicates in 'customers one'.

Result of the macro showing the removal of highlighted duplicates.

Encouragement to check out other video tutorials and the YouTube channel.

Transcripts

play00:01

hello and welcome to this video tutorial

play00:03

from computer ggar.com in this video we

play00:07

are going to look at how to use Excel

play00:10

VBA to compare two lists and to remove

play00:15

any duplicates any matching

play00:18

items so I have two worksheets of this

play00:22

workbook two customer sheets this is

play00:25

customers one and this is the one that I

play00:28

want to remove the duplicates

play00:31

from and just for the ease of this

play00:34

demonstration I've changed the color of

play00:37

the duplicate customers so don't be

play00:40

misled here you don't have to change the

play00:42

color of them for this to work I've only

play00:44

done that so that we know what to expect

play00:47

from this video and what the ultimate

play00:49

result will

play00:50

be so that is Lawrence Philipe Jose and

play00:55

Rita now if we look at customers 2 it's

play00:58

a slightly larger list so those four

play01:01

customers I just mentioned are in this

play01:03

list somewhere there's R for example

play01:06

they're in this list somewhere they are

play01:08

the duplicates there's

play01:10

Jose so they're the ones I'm going to

play01:12

remove anyone else must be a new

play01:16

customer I guess in this pretend

play01:20

scenario okay now the macro code we're

play01:23

going to need is already been written

play01:26

I'm just going to pop over there now and

play01:28

going to talk you through the key

play01:30

elements of it so let me click the

play01:33

developer tab on my ribbon and Visual

play01:37

Basic on the far left there and this is

play01:41

our VBA code now I'm going to provide

play01:45

this file via a link in the description

play01:48

of this video so check out the

play01:51

description below I'm going to have a

play01:53

link to this exact file so you can come

play01:55

and you can take this code play around

play01:58

with it learn from it use it

play02:00

uh whatever you wish it's going to be

play02:02

made available I'm also in the

play02:04

description of this video going to put a

play02:06

link to my online EXL VBA course which

play02:10

you can get for just

play02:13

$49 so if this is a little bit much for

play02:15

you but you really want to learn Excel

play02:17

VBA to automate your procedures like

play02:21

this then that's also worth checking out

play02:24

and uh we'll learn that together get

play02:26

through that together okay this is the

play02:29

code for now so I've declared a few

play02:31

variables at the top uh two for the

play02:35

worksheets customers one customers 2 two

play02:38

to track the row numbers customers one

play02:40

customers two one to identify how many

play02:43

customers are in that second uh

play02:48

list and then I've got uh one to track

play02:51

the ID I'm currently searching for and

play02:53

one just to report back how many

play02:56

duplicates so what we've got here we've

play02:58

got the initial kind of Declaration and

play03:00

stuff here this one finds out how many

play03:03

customers are in the second list so the

play03:05

one that I'm kind of checking against

play03:08

and then we start off our Loops which if

play03:11

you've done some Excel VBA you knew that

play03:13

there would be Some Loops involved in a

play03:15

task like

play03:17

this we have the external Loop which is

play03:20

this do while loop you see the words do

play03:23

while and then loop at the bottom this

play03:25

our external Loop this one is looping

play03:28

through the first let me uh shrink this

play03:31

a little bit this first customers list

play03:35

so starting from Maria then frederique

play03:37

then Lawrence then Peter and so on and

play03:40

so forth that's the one that's switching

play03:42

between those so initially it assigns

play03:47

the ID of the first person Maria that AF

play03:51

Alf Ki assigns it to this variable and

play03:55

then uses that to check against it

play03:59

it jumps into the second Loop customers

play04:02

2 so it switches over to this

play04:05

sheet and starts from row

play04:08

two which is way up here and starts

play04:11

looking for Maria in that scenario so we

play04:15

start from that custom ID and start

play04:17

looking for each sale looking for

play04:21

Maria if it finds Maria then the first

play04:26

sheet is activated the row is removed

play04:30

uh a note is made that one was removed a

play04:34

duplicate was found and I've just done a

play04:37

minus one on this row number now the

play04:39

reason for that is that you know on this

play04:43

first worksheet as we're looking for

play04:46

each person once we've of found or not

play04:49

found Maria we need to move to

play04:51

frederique we need to move to Lawrence

play04:54

but in a case of when we find Lawrence

play04:56

that row is going to be

play04:58

deleted and whenever you delete a row in

play05:01

Excel the other rows are going to move

play05:03

up so Peter Franken in row five will

play05:06

move into row

play05:07

four so when it comes to removing

play05:11

somebody I don't want to add a new row

play05:15

on which is this line down here like

play05:18

okay they're not in the list move to the

play05:20

next row I don't want to move to the

play05:21

next row because if I've deleted it the

play05:23

other row is going to move up into that

play05:26

place so I'll just kind of put a

play05:29

negative so when it gets to here and

play05:30

adds it on it effectively counteracts it

play05:34

it doesn't it doesn't increase in row

play05:37

numbers that increases the active sheet

play05:39

and that exits the four Loop down here

play05:42

so it comes out of this Loop really for

play05:44

the next customer to look

play05:46

for now I know I'm kind of shooting

play05:48

through this doing some

play05:50

explanation uh so you know if you've

play05:52

done some VBA before this will be

play05:54

sufficient for you and you'll understand

play05:56

a bit of what I'm saying and now I'm

play05:59

just trying to explain the key bits if

play06:01

not this video is probably going to be a

play06:03

bit much which why I recommend the the

play06:05

course in the description of this video

play06:07

or to to kind of understand a little bit

play06:10

about variables and about

play06:12

looping um so this video will make a

play06:15

little bit a kind of sense for you

play06:17

essentially though if this is something

play06:19

you need to do just follow the link in

play06:21

the description to get this file and you

play06:24

can take and use this code uh it should

play06:26

be ready to adapt for your needs to an

play06:30

extent as long as the kind of structure

play06:32

of it is similar to what I have right

play06:35

now at the end here just have a message

play06:37

box telling me that the duplicates have

play06:39

been removed so let's see this in action

play06:42

I told you there were four

play06:44

duplicates if I go to my macros button

play06:48

here's my macro ideally I'd have a

play06:50

button to click but I want to

play06:52

demonstrate using it this way and if I

play06:55

run this

play06:56

macro it simply removes those four would

play07:00

be helpful actually if I went back to

play07:01

the other sheet looking at this tells me

play07:02

four duplicates were removed I click

play07:06

okay I go back to customers one and

play07:09

those ones that are highlighted in that

play07:11

kind of orange color are no longer there

play07:15

so the job has been done they have been

play07:18

removed I hope you enjoy this video

play07:21

please check out some of our other video

play07:24

tutorials on our YouTube channel come

play07:26

check us out at computer.com

Rate This

5.0 / 5 (0 votes)

Related Tags
Excel VBATutorialRemove DuplicatesMacrosAutomationBeginner GuideExcel TipsData ManagementProgrammingVBA Course