Write Your First VBA Macro in Excel

Kyle Gibson
6 min readJan 18, 2023

--

Writing macros can make things in Excel much easier. And as an added bonus, you get to impress everyone when you drop technical jargon like “I wrote a macro to do that”. It’s a win-win!

This article will take you step-by-step through the process to write your own macro.

Step 1: Make sure you have the Developer tab available

Check the ribbon at the top of your Excel workbook, and see if the Developer tab is available:

Ribbon at top of Excel

If it is not available for you, same as me, then you’ll need to correct that.

Navigate to File > Options:

File button in ribbon
Options button under File button

Select Customize Ribbon, check the Developer box, and click OK.

Excel options window to customize ribbon and add Developer tab

You should now see the Developer tab:

Developer tab now showing in Excel ribbon

Step 2: Create the sample data to add two columns of numbers

For this example, our macro will add the numbers from two columns. Enter some sample data like this:

sample data — first number column, second number column, and total column

If you were adding these by using Excel formulas, you would probably do something like this:

Option 1: =Sum(A2, B2)

Example Excel formula to sum columns using SUM function

Option 2: =A2 + B2

Example example formula to sum columns using the plus sign

Then you would drag that formula down to row 4 to populate the rest of the rows, and end up with something like this:

Total column populated after dragging sum formula down in Excel

However, if you ended up adding more values to the First Number and Second Number columns, you would need to remember to drag the formula down again to calculate for that row:

Added new values for first number and second number columns, showing that Total would be blank for that row until you drag the formula down

In the following steps, you’ll create a macro instead to do this, and see if it makes things easier.

Clear out your total column, so that your sample data is back to looking like this:

sample data — first number column, second number column, and total column

Step 3: Create a module in VBA

Select the Developer tab:

Select Developer tab in ribbon

Select Visual Basic:

Select Visual Basic under Developer tab

With the Microsoft Visual Basic for Applications window open, select Insert > Module:

Insert module

A name like Module1 will be automatically given to this module:

Module1 Properties pane

You can rename the module in the properties pane. Delete the Module1 value and rename it to something else. I chose Example for my module name:

Module1 renamed to Example properties pane

You now have a module that can be used to write your macro.

Step 4: Write the macro

Right-click your new module, and select View Code:

View code for module

In the code editor that opens up, add in the following to create a macro:

Sub AddTwoNumbers()

End Sub

There is no logic yet added to this macro, but this does create a macro called AddTwoNumbers:

New macro called AddTwoNumbers with no code yet

In between the first line and the last line is where the logic for our macro will go.

Here is the full logic you can use to create the macro to add the two columns:

Sub AddNumbers()

'Declare variables
Dim row_count As Integer
Dim row_count_without_header As Integer
Dim row_iterator As Integer
Dim row_number As Integer

'Get total row count
row_count = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row

'Get total row count without header
row_count_without_header = row_count - 1

'Iterate/loop through rows
For row_iterator = 1 To row_count_without_header
row_number = row_iterator + 1
Sheet1.Cells(row_number, 3) = Sheet1.Cells(row_number, 1) + Sheet1.Cells(row_number, 2)
Next iterator

End Sub

Notes:

  1. First, declare the variables and their data types that you will use. In this example, I used four different variables.
  2. The row_count variable is looking at the first column to see how many total rows are in that column. This helps us know how many rows will need to be added. I am referencing Sheet1 in use case, since that is what the sheet is named for my sample data. (Note: the logic used to get that value is very handy. Keep that in mind for future usage)
  3. The row_count_without_header variable is subtracting one from row_count to give us the actual number of rows we need to sum, since the first column is the header and doesn’t need to be summed.
  4. The Sheet1.Cells formula takes two arguments — the first one is the Row Index and the second one is the Column Index. As we iterate through the rows, we are incrementing the row_number each time. As you can see in the For/Next loop, you are setting the value of column 3 equal to the value of columns 1 and 2 added together.

The full macro:

Full AddTwoNumbers macro

You can now close out of the VBA editor to go back to the main Excel view:

Close out of VBA editor

Step 5: Run the macro

Under the Developer tab, select Macros:

Macros button under Developer tab

Select the AddTwoNumbers macro just created, and click Run:

Run Macro window

Look at your sample data now, and you can see that the Total column is now populated with data; also note that the formula bar is populated with the value, not an Excel formula:

Sample data with total column now populated

Our macro summed both columns successfully.

Step 6: Test the macro

Let’s test a couple of scenarios with our macro to see how useful it could be.

Additional fourth row to sample data with no value for total

Add an extra row like this to your sample data, and then run the macro again. It should look like this:

Sample data with additional row and total column is now populated

Now delete the Total value for the middle two rows…

Removed data from middle two rows for total

…and run the macro again. It should look like this:

Full populated sample data with total

Your macro has enabled you to be very flexible now. You don’t have to wonder if the Excel formula copied correctly or panic if the formula got deleted or corrupted. You can run that macro any time and it will fix and/or update that Total column.

Step 7: Save your workbook as a Macro-Enabled file

When saving this workbook, save it as an .xlsm (Excel Macro-Enabled Workbook) file to allow macros to run in the file:

Saving as xlsm file option

And that’s it!

Now you can tell everyone that you know how to write a macro. Hopefully this inspires you to search for ways to automate some of your Excel work in the future.

Thanks for reading!

--

--

Kyle Gibson
Kyle Gibson

Written by Kyle Gibson

Christian, husband, father. Data Engineer at Chick-fil-A.

Responses (1)