How To Use Excel VBA Code – Real Example S2

How To Use Excel VBA Code – Real Example S2

How To Use Excel VBA Code To Speed Up Complex Manual Work

Welcome to the Excel VBA Real Example Series – season 2. If you are interested in using Excel VBA to speed up manual work, this series is for you.

In the Excel VBA Real Example Series, we take a file from a viewer of the channel and demonstrate how to use Excel VBA to get an annoying manual task done ‘at the click of a button’. Yes, with Excel VBA you can get those jobs (that you are spending minutes, hours or days completing) done at in just a few seconds, such is the power of Excel VBA. So, let’s get into it!

Click here to download the Excel files for this series.


Thank you to channel viewer Chahin for sending in this example. We’ve selected it because it’s typical of so many examples of Excel-based work that we have seen over the years.

We have an old and a new data file, and the question is simple: which entries appear in the old file, which entries appear in the new, and which entries appear in both? Then, the entries should be checked against a product data file for any inaccuracies in the record. It’s typical of manual work in Excel that can be frustrating. We’re using a small dataset for learning purposes, but this kind of task would be a nightmare with a larger dataset, so let’s learn how to use Excel VBA to get it done.

Part 1 – Planning and Conceptualisation



In part 1, we discuss the task at hand, and do the all-important planning and conceptualisation. This may not sound very exciting and many people miss out this stage altogether in their programming; to do so often creates problems later. We argue that conceptual rather technical issues account for most of the problems people experience with VBA code.

Do you get the concept? Can you explain in plain English (or your own language) how the code should work? If you cannot, you are likely to run into problems later.

In the first video, therefore, we make sure the concepts are clear by using ‘pseudo-code’ (descriptive comments in the VBA editor) to work through the task ‘step-by-step’, making notes as we go. This lays an important foundation: when we begin coding (which we promise we will start in the next video!) these comments (or ‘annotations’) act as an important guide, saving mental bandwith for the technical component – in other words – the actual VBA coding.

Make sure you download the three files and work along with Chris – can you get a clear idea in your head how this routine is likely to work? What are the key features – loops, conditional statements etc.? Try applying this deliberate planning approach to your day-to-day VBA coding – we are confident you will see improvements, and feel lower stress levels!

Part 2 – Interacting With Multiple Files With Excel VBA



In video 2 of the Excel VBA Real Example series (season 2), we look at how to interact with multiple files using Excel VBA. Things tend to get very complicated very quickly when working with multiple files. So, it’s sensible to implement checks before the code runs to check that the right files (and the right number of files) are open. This *should* help us avoid complete chaos later!

We look at how to do this by harnessing the Workbooks collection. First, we ask the VBA editor how many files (or objects) are in the collection; in other words, how many files are open? Then, we loop through the collection, checking against the cells in the spreadsheet and the name of the file that contains the code, to ensure the correct files are open.

Only if these conditions are met do we proceed with the coding to do the analysis on the files. At all times, we make sure the user is informed by using (dynamic) message boxes to convey important information; which file is problematic, for example? We highlight this as one way to make yourself stand out as a programmer: effective communication with the user.

Did you manage to get the code working too? See you in the next video!

Part 3 – A Sophisticated File Validation Mechanism




In video 3 of the series, we look at how to create a sophisticated validation mechanism to check the right files are open – and no others! We use VBA to check through the open files and ensure each (that is ‘supposed’ to be open) is open in order to complete the task. This involves some advanced coding: we have to implement an IF statement with multiple conditions, for example, and use another dynamic message box to inform the user of what is wrong should there be a problem. Remember – sophisticated user communication is a great way to make you stand out as a programmer.

The structure we set up is what Chris uses on all Excel VBA projects involving multiple files. These projects can be complicated; one way to manage the complexity is to ensure that the right files are open (and closed!) This should keep the stress levels down and allow us to focus on creating value for the client rather than ‘getting things to work’.

Part 4 – Time To Tidy Up!



In the fourth installment in the series, we deal with the non-glamorous but essential job of ‘tidying up’. Yes, alongside planning and a bundle of other skills, tidying up is one of the key competencies that nobody really talks about – but which determine your effectiveness as a coder. Don’t worry, you can learn all of these skills, either implicitly or explicitly, on the Tiger channel!

The key idea is that, by keeping stress levels down, coding sessions can be longer and more productive. Yes, we need to stay below the critical point where you want to throw the laptop out of the office window (we’ve all been there!) To do that, we need to be proactive. Let’s do ourselves a favour and take the chance to ‘tidy up’ right now.

In the video, Chris first shows how to use With … End With to economise on code; fewer characters to read means lower cognitive load and lower stress levels, so integrating With … End With represents a quick win for us. Just remember to add the full stops (.) where necessary.

Then we look at re-arranging the annotations (remember, the ones we created in the first video of the series?) so that each aligns with the correct line of code. Being able to understand what the code is doing in your native language is a real bonus for the coder – so let’s take the time now to get those annotations up-to-date.

A small test confirms everything is still working as required.

See you in the next video when we will tackle some more exciting coding ideas…

Part 5 – A Modular Approach



In recent years, I have experimented with a more ‘modular’ approach in my Excel VBA programming, getting good results. A more modular approach can mean easier-to-understand code and a lower risk of crashes – but how to actually realise it in practice? Let’s check it out in this video…

It seems we’ve reached the end of the first ‘phase’ of this task – checking the open files are present, open, and correctly named. We could call this phase ‘validation’ and make it ‘distinct’ or discrete by separating it from the main programming. We could then trigger this first phase from a separate button (as we will later in the series) to create a sense of a ‘pre-departure check’. Yes, this phase does not do much in terms of analysis; but it does set things up for us, and a phase completed successfully builds user confidence.

This approach requires a ‘module-level’ variable. These variables, declared at the top of the module, are not cleared after a routine executes; they continue to store their values and can be used to pass information between routines within the same module – cool! In the video, we create a module level variable, test it, and then use it to check a condition before triggering another routine – the modular approach in action.

As always, we include lots of testing as we go along. Our classic ‘flash up a message box’ approach comes in handy more than once; at the same time, we take the opportunity to improve a message box by changing its title to something that directs the user more effectively. Remember – user communication is everything!

So, are you a fan of the modular approach? Perhaps you can’t feel the benefits fully yet, but Chris does mention some in the video. As we work through the task, the approach will take shape and hopefully you’ll be able to feel its full power.

See you in the next video in the Excel VBA Real World Example series – season 2! Don’t forget to leave Chris a comment below (he answers all comments personally.)

Part 6 – Step-By-Step



In the previous video we explored how to apply a modular approach in this implementation. In video 6 in the series, we first add buttons to the interface, to allow us to exploit the modularity of the code, and to allow the user to ‘work through’ the process step-by-step. It takes a little more time to implement a modular approach, but we will soon begin to feel the benefits: modulated code is easier to debug (it’s bound to go wrong at some point!)

Moreover, it allows your client to gradually build confidence in the application, and in you as a programmer. No more clicking the button then closing your eyes and hoping everything works!

Let’s not forget more practical matters. In the video, Chris shows how to create the buttons, align them with the spreadsheet gridlines, assign macros to them and change the text that displays on the button. Don’t forget to use the Alt key to make sure those buttons are aligned! (Chris’s bugbear.)
With our button in place, let’s take our communication with the user to the next level by including text on the interface to let them know when the file was last validated. We use some cool VBA syntax to get this done – NOW to give the current date and time, and FORMAT to display the text exactly as we require.

The step-by-step approach we apply in this series may seem unnecessarily time-consuming (Chris calls it ‘ponderous’ in the video.) Hopefully you are feeling some of the benefits already, however, and more will come apparent as we work through the series. Years of working on real-world projects has taught Chris this really is the best way!

See you in the next video and don’t forget to leave a comment on YouTube, Chris will get back to you.

Part 7 – A Remote Reference To Another File

We begin by testing the validation mechanism created in the previous video and refreshing our minds about how the module level variable works. Ultimately, we want the customer to be able to click through a few buttons to get this task done. Yes – it’s more ‘click-of-a-few-buttons’ than ‘click-of-a-button’ but we are arguing that the ‘modular’ approach delivers benefits for the user (by building confidence in the process) and the programmer by making the code easier to debug.

This takes us to the next phase of the task. So, what to do? Here, the time we took to talk through the task at the beginning (see video 1 in the series) and create pseudo code (the green annotations or comments) pays dividends. We can simply read through the annotations to find what to do next. No shifting between conceptual thinking and coding. Cool!

One thing we will have to do is remotely reference the other open files. ‘Remotely’ (as opposed to ‘directly) means referencing the files without activating them. It involves more sophisticated coding but results in far more efficient execution.

In the video, Chris talks about the value of the ‘ThisWorkbook’ construct – meaning ‘the workbook that contains this code’. It comes in useful when working with multiple open workbooks. As with multiple sheets, we have to make sure that Excel understands the object we are talking about when working with multiple workbooks. So, let’s try the syntax and test it using Msgbox, as usual.

In the video, Chris also shows how to integrate the With … End With construct to economise on code and improve efficiency. Are you using it in your coding practice?

We end the video by remotely referencing a cell in another open file to flash up the contents of a cell in a message box. It’s proof that our remote and modular coding approach works, at least for this simple example. In the next video, we’ll look at scaling up this approach to begin to create some insights for our client.

See you in the next video and don’t forget to leave a comment on YouTube, Chris will get back to you.

——————————————

Are you following season 2 of the Real Example VBA series? We would love to hear from you, and Chris answer all comments personally, so leave us your questions below in the comments.


Free Excel (VBA) Tutorials

Buy nowLearn more

Become An Excel Analyst In 30 Days (COMPLETE COURSE!)

  • 4 Ways To Get Better At Excel Today! (THE EXCEL METASKILLS)
  • How Do Companies ACTUALLY Use Excel In 2023?
  • Excel Simplified: The Things You NEED To Know In Excel (and what you can ignore)
  • How To Collect Data With Excel In 2023 (Without Annoying Your Coworkers!)
  • Power Query For Beginners
  • STOP Fixing Data In Excel! START Using Excel Data Validation Like A Pro ...
  • Top 10 Excel Formulae For Cleansing Your Data QUICKLY And EFFICENTLY In Excel
  • How To Do A Basic Data Analysis In Excel (CHART INCLUDED!)
  • My QUICK And EASY Method For Building An Interactive Dashboard In Excel
  • Why You Should Learn Excel’s Data Analysis Formulae (EXCEL PIVOT TABLE ALTERNATIVE!)
  • How To Create BEAUTIFUL Charts In Excel
  • What's An Excel 'Model'? (AND 5 EXCEL MODEL-BUILDING FORMULAE!)
  • The Secret To Smooth Formula-Building In Excel (EXCEL MODELLING FOR BEGINNERS!)
  • Build This BEGINNER EXCEL MODEL With Me In Around 15 Minutes!
  • How To Create BEAUTIFUL Visualisations For Excel Models
  • THE BASICS OF EXCEL VBA EXPLAINED (For Beginners!)
  • Frustrated With Recorded Macros In Excel VBA? HERE'S HOW TO IMPROVE THEM
  • 5 ESSENTIAL Excel VBA Beginner Techniques
  • How Automate Manual Work In Excel With Loops In Excel VBA
  • How To Generate UNLIMITED Random Data In Excel (CLICK OF A BUTTON!)
  • SAVE TIME IN EXCEL With These 23 Excel Keyboard Shortcuts (WINDOWS PC)
  • 5 Formatting Principles For Better Excel Spreadsheets
  • 4 Worksheets To Include In ALL Your Excel Files (GENERIC EXCEL SHEET ROLES!)
  • What's The Difference Between An ANALYST And A CONSULTANT? And Why Does It Matter For YOUR Career?
  • 4 NON-EXCEL Tools To Help You Create Better Spreadsheets
  • How Can DRAWING Help You Create Better Excel Spreadsheets?
  • How Should You Manage An Excel Development Project? (4 STEPS!)
  • How To Make Excel Your Career (IN FOUR STEPS!)
  • Can You Become An Excel Analyst In 30 Days? (COURSE OVERVIEW)

Articles topics

  • 5 Essential Excel Formulae To Start Your Journey
  • Excel Formatting For Absolute Beginners
  • What You've Been Getting Wrong About Excel
  • How To Use Excel Tables And Excel VBA To Automate Manual Work
  • 26 Excel Productivity Tips For 2026
  • EXCEL: STATE OF THE UNION (How To Save Your Excel-Related Career)
  • THE EXCEL NIGHTMARE
  • Excel VBA To Copy Data From One FILE To Another (BEGINNER TO PRO! PART 2/2)
  • How To Format An Excel Spreadsheet (CHECKLIST INCLUDED!)
  • How To Stop Data Leaks In Excel (8 MUST-KNOW EXCEL TIPS!)
  • Excel VBA To Copy Data From One FILE To Another (BEGINNER TO PRO! PART 1/2)
  • STOP PAYING! Use EXCEL Instead For These 7 Software Tasks
  • Excel Forms Beginner Tutorial
  • How To Collect Data In Excel In 2025 (STOP ANNOYING COWORKERS!)
  • MIC'D UP: EXCEL DEVELOPER TAKES ON THE EXCEL WORLD CUP
  • THE 5 LEVELS OF EXCEL AUTOMATION
  • How To Automate Powerpoint Creation With Excel VBA (INSTANT PRESENTATIONS!)
  • Excel Office Script for Beginners - Part 3 of 3: Create A Loop In Excel Office Script
  • Excel Office Script for Beginners - Part 2 of 3: Manipulate Cells With Office Script
  • EXCEL VBA MASTER TIP: The Best Way To Copy / Paste Data From One Sheet To Another In Excel?
  • Stop Obsessing Over Excel Formulas! YOU'RE MISSING THIS ...
  • Excel VBA To Copy Data From One File To Another (BEGINNER TO PRO!)
  • Don't Copy / Paste Data In Excel! Use These 5 Alternatives Instead ...
  • Excel VLOOKUP Formula For Beginners
  • Excel Dashboards For Beginners
  • 25 Powerful Excel VBA Techniques For 2025
  • The Excel Mistake That KILLS Small Businesses
  • Excel Office Script For Beginners (AND VBA ADDICTS!) Part 1/3
  • 3 Excel Calamities (FROM THE TIMES!) And How To Avoid Them
  • RIP: MY EXCEL VBA CAREER (2008-2024)
  • AVOID This Excel Formatting Mistake (DO THIS INSTEAD!)
  • INSANELY Simple Excel Formatting Trick That Works EVERY Time
  • THE ONE FORMULA I WISH EXCEL HAD (+How To Build It Yourself!)
  • The 30 Excel Formulae You Must Know In 2024 (1-HOUR MASTERCLASS!)
  • Power Automate For BEGINNERS
  • Learn Macros In 19 Minutes (Microsoft Excel)
  • Learn Excel's BEST Automation Tool In 15 Minutes!
  • Learn The Easy Way To Transfer Data With Excel VBA In Around 5 Minutes! (WITH DOWNLOAD FILES)
  • How To Do What VLOOKUP Can't Do With OFFSET and MATCH
  • How To Categorise Numbers In Excel Using VLOOKUP
  • When You SHOULD Use INDIRECT
  • How To Remove Spaces in Excel (When TRIM Doesn’t Work!)
  • Complete Manual Excel Tasks In Seconds With THIS Excel Formula
  • Learn This Powerful Excel Formula Combination In Just 5 Minutes!
  • How To Model Winning And Losing Streaks In Excel
  • BEYOND THE MACRO RECORDER
  • Why Short Excel Formulae Are Better Than Long Excel Formulae (WITH PRACTICAL DEMO)
  • 17 Excel Beginner Formatting Mistakes (AND HOW TO FIX THEM!)
  • How To Format Your Excel Spreadsheet Quickly And Professionally With 26 Excel Keyboard Shortcuts
  • Excel Conditional Formatting Tutorial (FROM BEGINNER TO PRO!)
  • How Excel Knows If You're Working On Mac Or PC
  • 14 MORE Excel Formatting Tips (Make Excel Beautiful AGAIN)
  • Excel's Most Powerful Data Analysis Formula Explained In 5 Minutes
  • How To Use Conditional (IF) Statements In Excel VBA (Real-World VBA Task S3 P6)
  • Get Started With Position Control In Excel VBA (Real-World VBA Task S3 P5)
  • How To Combine Two Loop Types In Excel VBA (Real-World VBA Task S3 P4)
  • How To Build A Loop Within Loop In Excel VBA (Real-World VBA Task S3 P3)
  • How To Build A Loop In Excel VBA (Real-World VBA Task S3 P2)
  • How To Start An Excel VBA Macro (Real-World VBA Task S3 P1)
  • How To Analyse Goal Distribution In Excel (BEGINNER EXCEL DATA ANALYSIS!)
  • 3 Alternatives To SUBTOTAL To Analyse Football Data In Excel (PLEASE STOP!)
  • Complete Guide To Excel For Football Traders
  • Why I Don't Use Excel Tables
  • Don’t Just Use =AVERAGE In Your Excel Data Analysis! Do This Instead
  • 14 Excel Formatting Tips (Make Excel BEAUTIFUL!)
  • How To Automate VLOOKUP With Excel VBA
  • Can We Get Excel To Play Chess With Excel VBA? (INFINITY PROJECT)
  • How To Analyse Bitcoin ($BTC) Price In Excel
  • 10 Ways My Excel VBA Has Improved In 10 Years (BEGINNER TO PRO!)
  • Make Excel VBA Loops EASY With The Loop Blueprints (MM LITE #5)
  • Excel VBA Arrays For Beginners
  • The 3 Excel Superpowers
  • Even Beginners Can Learn Loops In Excel VBA ... (MM LITE #4)
  • The Only 3 IF Statements You Need In Excel VBA - Excel VBA Beginner Fundamentals (6/6)
  • Beginner Survival Guide To Working With Dates And Times In Excel
  • Create An Excel Combo Chart In 7 Easy Steps (MM LITE #3)
  • My No1 Excel VBA Technique Explained In 10 Minutes
  • The BEST Way To Move Sheets Between Files In Excel (MM LITE #2)
  • Master Excel's 11 Logical Formulae
  • How To Create A Loop Within A Loop In Excel VBA (MM LITE #1)
  • I Tried To Learn 500 Excel Formulae
  • Get Started With Loops In Excel VBA
  • Beginner Introduction To Variables In Excel VBA
  • 10 Popular Excel Formulae: OVERRATED / UNDERRATED?
  • How To Work With Cells With Excel VBA
  • Excel VBA Beginner Fundamentals
  • 10 Excel Formula Building Tips (Windows PC)
  • 7 Useful Excel Formulae You've Never Heard Of
  • What's The Best Excel Chart For Beginners?
  • Excel Data Analysis For Beginners: Learn This Formula FIRST
  • Why I Don't Use Pivot Tables In Excel
  • The ONE Excel Beginner Formula That Does ALL This
  • The 72 Things You Need To Know In Excel In 7 Minutes 2 Seconds
  • Let's Fix 5 Shocking Excel Pie Charts (SAVE A FRIEND!)
  • Should You Learn Excel VBA In 2021? 7 Questions To Answer
  • Excel VBA To Copy Data From One Sheet To Another (BEGINNER TO PRO!)
  • XLOOKUP For Beginners (And Why I'm Sceptical About New Excel Formulae ...)
  • Can’t Do Data Analysis In Excel? Just Do This!
  • Make Your Excel Data Chartable With This EPIC Excel Formula
  • Replace Excel Filtering ... With An Excel FORMULA?
  • The Best Alternative To Excel Filters?
  • Don't Use Excel Filters! Use This Incredible Excel Formula Instead ...
  • How To Validate Userform Inputs With Excel VBA
  • 3 Ways To Insert A Reference No. Using An Excel Userform
  • 3 Excel Data Analysis Techniques You Should Be Using
  • Excel VBA For Beginners: 30 Real World Problems And Solutions
  • How To Make Money From Excel
  • Excel VBA Absolute Beginner Course 30 For 30
  • Excel VBA Position Control Mastery
  • 9 Reasons (NOT) To Do An MBA
  • Suffering From Excel Ennui? Why It's Not (All) Your Fault
  • How To Use Excel VBA Code – Real Example S2
  • How To Get Better At Excel: The Excel Metaskills
  • Excel Dashboards For Beginners
  • 7 Essential Excel VBA Beginner Techniques
  • 17 Things You Must Know About The Excel VBA Editor
  • How to Change Grades to Numbers in Excel
  • How To Match A Number To A Range in Excel
  • How To Use Excel VBA Code – Real Example
  • Excel Indirect Function Tutorial
  • Excel Data Table Tutorial
  • Excel VBA to Move Data Around a File – My Approach
  • Your Weird Skillset is Your Best Asset
  • .Cells For Beginners
  • Your First Pivot Table
  • 19 Excel Beginner Shortcuts for 2019
  • Understand an Excel File in 3 Steps
  • Most Important Skill in Excel VBA
  • VLOOKUP Formula Problems / Fixes
  • Excel VBA to Copy Data to Sheets
  • Excel Frequency Formula: Common Problems / Fixes
  • Excel VBA to Convert Minutes to Decimals
  • What are Excel VBA Variables?
  • Use Excel VBA to Separate Worksheets to Files
  • Working with Excel VBA Message Boxes
  • Excel VBA IF Conditional Statements for Beginners
  • Excel Learning Tips: My Story
  • Excel Database Formulae For Beginners
  • Spreadsheet Skunkworks! Excel VBA Chess Challenge
  • Excel Userforms for Beginners
  • Use Excel VBA to Create Spin Buttons – Really Precisely!
  • Use Excel VBA to Create Buttons For Super Fast Data Input!
  • How to Collate Sports Fixtures Results into a League Table in Excel
  • Excel Custom Navigation: Excel VBA for Beginners
  • Excel Game – Do You Know the Offside Rule?!
  • Rich Picturing: Six Tips for Getting Started
  • Excel VBA for Beginners – Use Excel VBA to Cleanse a Dataset
  • Excel Offset Function – Give It Some Love!
  • Excel Visual Basic (VBA) for Post-Beginners
  • Generate Random Data in Excel
  • Combining VLOOKUP and MATCH
  • Dynamic Dropdown Menus in Excel
  • Excel for Teachers: Create A Student Progress Tracker
  • Excel Visual Basic (VBA) for Beginners
  • Create A League Table in an Excel Spreadsheet
  • What is Excel Spreadsheet Development?
  • Rank Things and Create A League Table in Excel
  • Finding Things in Lists of Data in Excel
  • #ExcelRevolution Manifesto
  • Cool Excel Formatting: Font Size and Colour
  • #ExcelRevolution Introduction
  • Combining MATCH and OFFSET
  • Cool Excel Formulae: COUNTIF
  • Cool Excel Formatting: Dynamic Headers
  • Cool Excel Formulae: IF
  • Cool Excel Formatting: Cell Borders
  • Cool Excel Formulae: VLOOKUP
  • Cool Excel Formulae: MATCH
  • Cool Excel Formulae: OFFSET
  • Cool Excel Formulae: COUNTA
  • Cool Excel Formulae: IFERROR