How to Write Code in a Spreadsheet
2 JUNE 2020
Last updated
2 JUNE 2020
Last updated
This is a tutorial about writing code in Excel spreadsheets using Visual Basic for Applications (VBA).
Excel is one of Microsoftâs most popular products. In 2016, the CEO of Microsoft said "Think about a world without Excel. That's just impossible for me.â Well, maybe the world canât think without Excel.
In 1996, there were over 30 million users of Microsoft Excel (source).
Today, there are an estimated 750 million users of Microsoft Excel. Thatâs a little more than the population of Europe and 25x more users than there were in 1996.
Weâre one big happy family!
In this tutorial, youâll learn about VBA and how to write code in an Excel spreadsheet using Visual Basic.
You donât need any prior programming experience to understand this tutorial. However, you will need:
Basic to intermediate familiarity with Microsoft Excel
If you want to follow along with the VBA examples in this article, you will need access to Microsoft Excel, preferably the latest version (2019) but Excel 2016 and Excel 2013 will work just fine.
A willingness to try new things
Over the course of this article, you will learn:
What VBA is
Why you would use VBA
How to get set up in Excel to write VBA
How to solve some real-world problems with VBA
Here are some important concepts that you should be familiar with to fully understand this tutorial.
Objects: Excel is object-oriented, which means everything is an object - the Excel window, the workbook, a sheet, a chart, a cell. VBA allows users to manipulate and perform actions with objects in Excel.
If you donât have any experience with object-oriented programming and this is a brand new concept, take a second to let that sink in!
Procedures: a procedure is a chunk of VBA code, written in the Visual Basic Editor, that accomplishes a task. Sometimes, this is also referred to as a macro (more on macros below). There are two types of procedures:
Subroutines: a group of VBA statements that performs one or more actions
Functions: a group of VBA statements that performs one or more actions and returns one or more values
Note: you can have functions operating inside of subroutines. Youâll see later.
Macros: If youâve spent any time learning more advanced Excel functionality, youâve probably encountered the concept of a âmacro.â Excel users can record macros, consisting of user commands/keystrokes/clicks, and play them back at lightning speed to accomplish repetitive tasks. Recorded macros generate VBA code, which you can then examine. Itâs actually quite fun to record a simple macro and then look at the VBA code.
Please keep in mind that sometimes it may be easier and faster to record a macro rather than hand-code a VBA procedure.
For example, maybe you work in project management. Once a week, you have to turn a raw exported report from your project management system into a beautifully formatted, clean report for leadership. You need to format the names of the over-budget projects in bold red text. You could record the formatting changes as a macro and run that whenever you need to make the change.
Visual Basic for Applications is a programming language developed by Microsoft. Each software program in the Microsoft Office suite is bundled with the VBA language at no extra cost. VBA allows Microsoft Office users to create small programs that operate within Microsoft Office software programs.
Think of VBA like a pizza oven within a restaurant. Excel is the restaurant. The kitchen comes with standard commercial appliances, like large refrigerators, stoves, and regular oleâ ovens - those are all of Excelâs standard features.
But what if you want to make wood-fired pizza? Canât do that in a standard commercial baking oven. VBA is the pizza oven.
Yum.
Because wood-fired pizza is the best!
But seriously.
A lot of people spend a lot of time in Excel as a part of their jobs. Time in Excel moves differently, too. Depending on the circumstances, 10 minutes in Excel can feel like eternity if youâre not able to do what you need, or 10 hours can go by very quickly if everything is going great. Which is when you should ask yourself, why on earth am I spending 10 hours in Excel?
Sometimes, those days are inevitable. But if youâre spending 8-10 hours everyday in Excel doing repetitive tasks, repeating a lot of the same processes, trying to clean up after other users of the file, or even updating other files after changes are made to the Excel file, a VBA procedure just might be the solution for you.
You should consider using VBA if you need to:
Automate repetitive tasks
Create easy ways for users to interact with your spreadsheets
Manipulate large amounts of data
To write VBA, youâll need to add the Developer tab to the ribbon, so youâll see the ribbon like this.
To add the Developer tab to the ribbon:
On the File tab, go to Options > Customize Ribbon.
Under Customize the Ribbon and under Main Tabs, select the Developer check box.
After you show the tab, the Developer tab stays visible, unless you clear the check box or have to reinstall Excel. For more information, see Microsoft help documentation.
Navigate to the Developer Tab, and click the Visual Basic button. A new window will pop up - this is the Visual Basic Editor. For the purposes of this tutorial, you just need to be familiar with the Project Explorer pane and the Property Properties pane.
First, letâs create a file for us to play around in.
Open a new Excel file
Save it as a macro-enabled workbook (. xlsm)
Select the Developer tab
Open the VBA Editor
Letâs rock and roll with some easy examples to get you writing code in a spreadsheet using Visual Basic.
In the VBA Editor, select Insert -> New Module
Write this code in the Module window (donât paste!):
Save, close the workbook, and reopen the workbook. This dialog should display.
Ta da!
Depending on your familiarity with programming, you may have some guesses. Itâs not particularly complex, but thereâs quite a lot going on:
Sub (short for âSubroutine): remember from the beginning, âa group of VBA statements that performs one or more actions.â
Auto_Open: this is the specific subroutine. It automatically runs your code when the Excel file opens - this is the event that triggers the procedure. Auto_Open will only run when the workbook is opened manually; it will not run if the workbook is opened via code from another workbook (Workbook_Open will do that, learn more about the difference between the two).
By default, a subroutineâs access is public. This means any other module can use this subroutine. All examples in this tutorial will be public subroutines. If needed, you can declare subroutines as private. This may be needed in some situations. Learn more about subroutine access modifiers.
msgBox: this is a function - a group of VBA statements that performs one or more actions and returns a value. The returned value is the message âWelcome to the XYZ Workbook.â
In short, this is a simple subroutine that contains a function.
Maybe you have a very important file that is accessed infrequently (say, once a quarter), but automatically updated daily by another VBA procedure. When it is accessed, itâs by many people in multiple departments, all across the company.
Problem: Most of the time when users access the file, they are confused about the purpose of this file (why it exists), how it is updated so often, who maintains it, and how they should interact with it. New hires always have tons of questions, and you have to field these questions over and over and over again.
Solution: create a user message that contains a concise answer to each of these frequently answered questions.
Use the MsgBox function to display a message when there is any event: user closes an Excel workbook, user prints, a new sheet is added to the workbook, etc.
Use the MsgBox function to display a message when a user needs to fulfill a condition before closing an Excel workbook
Use the InputBox function to get information from the user
In the VBA Editor, select Insert -> New Module
Write this code in the Module window (donât paste!):
Save and navigate back to the Developer tab of Excel and select the âButtonâ option. Click on a cell and assign the UserReportQuery macro to the button.
Now click the button. This message should display:
Click âyesâ or hit Enter.
Once again, tada!
Please note that the secondary subroutine, ProcessReport, could be anything. Iâll demonstrate more possibilities in example #3. But first...
This example builds on the previous example and has quite a few new elements. Letâs go over the new stuff:
Dim UserInput As Long: Dim is short for âdimensionâ and allows you to declare variable names. In this case, UserInput is the variable name and Long is the data type. In plain English, this line means âHereâs a variable called âUserInputâ, and itâs a Long variable type.â
Dim Answer As Integer: declares another variable called âAnswer,â with a data type of Integer. Learn more about data types here.
UserInput = vbYesNo: assigns a value to the variable. In this case, vbYesNo, which displays Yes and No buttons. There are many button types, learn more here.
Answer = MsgBox(âProcess the XYZ Report?â, UserInput): assigns the value of the variable Answer to be a MsgBox function and the UserInput variable. Yes, a variable within a variable.
If Answer = vbYes Then ProcessReport: this is an âIf statement,â a conditional statement, which allows us to say if x is true, then do y. In this case, if the user has selected âYes,â then execute the ProcessReport subroutine.
This could be used in many, many ways. The value and versatility of this functionality is more so defined by what the secondary subroutine does.
For example, maybe you have a file that is used to generate 3 different weekly reports. These reports are formatted in dramatically different ways.
Problem: Each time one of these reports needs to be generated, a user opens the file and changes formatting and charts; so on and so forth. This file is being edited extensively at least 3 times per week, and it takes at least 30 minutes each time itâs edited.
Solution: create 1 button per report type, which automatically reformats the necessary components of the reports and generates the necessary charts.
Create a dialog box for user to automatically populate certain information across multiple sheets
Use the InputBox function to get information from the user, which is then populated across multiple sheets
For loops are very useful if you need to perform repetitive tasks on a specific range of values - arrays or cell ranges. In plain English, a loop says âfor each x, do y.â
In the VBA Editor, select Insert -> New Module
Write this code in the Module window (donât paste!):
Save and navigate back to the Developer tab of Excel and select the Macros button. Run the LoopExample macro.
This should happen:
Etc, until the 100th row.
Dim X As Integer: declares the variable X as a data type of Integer.
For X = 1 To 100: this is the start of the For loop. Simply put, it tells the loop to keep repeating until X = 100. X is the counter. The loop will keep executing until X = 100, execute one last time, and then stop.
Range("A" & X).Value = X: this declares the range of the loop and what to put in that range. Since X = 1 initially, the first cell will be A1, at which point the loop will put X into that cell.
Next X: this tells the loop to run again
The For-Next loop is one of the most powerful functionalities of VBA; there are numerous potential use cases. This is a more complex example that would require multiple layers of logic, but it communicates the world of possibilities in For-Next loops.
Maybe you have a list of all products sold at your bakery in Column A, the type of product in Column B (cakes, donuts, or muffins), the cost of ingredients in Column C, and the market average cost of each product type in another sheet.
You need to figure out what should be the retail price of each product. Youâre thinking it should be the cost of ingredients plus 20%, but also 1.2% under market average if possible. A For-Next loop would allow you to do this type of calculation.
Use a loop with a nested if statement to add specific values to a separate array only if they meet certain conditions
Perform mathematical calculations on each value in a range, e.g. calculate additional charges and add them to the value
Loop through each character in a string and extract all numbers
Randomly select a number of values from an array
Now that weâve talked about pizza and muffins and oh-yeah, how to write VBA code in Excel spreadsheets, letâs do a learning check. See if you can answer these questions.
What is VBA?
How do I get set up to start using VBA in Excel?
Why and when would you use VBA?
What are some problems I could solve with VBA?
If you have a fair idea of how to you could answer these questions, then this was successful.
Whether youâre an occasional user or a power user, I hope this tutorial provided useful information about what can be accomplished with just a bit of code in your Excel spreadsheets.
Happy coding!
Excel VBA Programming for Dummies, John Walkenbach
Reference : https://www.freecodecamp.org/news/excel-vba-tutorial/