Excel Macro Examples

Here is a sneak preview of how much you can accomplish using macros in Excel!
How to Create Your First Macro Button in Excel
Do you repeat various Excel actions like copy & pasting data, formatting, putting borders etc?
Do you know that you can automate your tasks in Excel with a couple of mouse clicks?
If you keep on repeating the same thing over and over again, creating an Excel Macro is perfect for you! It saves you time so you can do things that you like doing, like going home early and spending time with your family :)
Click a button and all your tasks are done for you! Get ready create your first macro in Excel!
I will show you step by step, how to enable the Macro Creation on your computer and create your very first Excel Macro Button!
STEP 1: Right click anywhere on your Ribbon and select Customize the Ribbon.
We want to make sure the Developer Tab is shown in your Excel ribbon.
STEP 2: Make sure the Customize Ribbon is selected.
Then select the Developer option under Main Tabs.
Click OK.
You should now see the Developer Tab in your ribbon.
STEP 3: Now go to Developer > Insert > ActiveX Controls > Button
Thought Excel was only for inputting data? Guess again :)
Drag anywhere in the Excel spreadsheet to create your first Button!
STEP 4: Right click on your new button and select View Code.
STEP 5: Type in this code: Range(“A6”).Value = “Hello”
What this will do is to set the value of cell A6 to Hello.
This is how to create a macro in Excel, then we are tying to our newly created button.
STEP 6: Time to test it out! Make sure Design Mode is deselected.
STEP 7: Click on the button now and you will see Hello pop up on your screen! Congratulations on executing your first Excel macro as well!
How to Use Macro Recorder in Excel
Sometimes we want to automate some steps in Excel to save us time, however it’s cumbersome to code the exact macro for it – especially as a VBA beginner.
But you know what? There is an easy way around :)
You can try out the Macro Recorder to mimic each step inside Excel and reuse those steps next time around. How cool is that?!
Make sure your Excel ribbon has the Developer Tab enabled as explained above.
Turn on the Macro Recorder, do the steps, stop the Macro Recorder, run the Macro and bam! Excel will repeat the exact same steps you just did!
You can also use this to reverse engineer and see how it looks like in code. Because Excel generates the code for you, you can check the recorded steps afterwards so that you learn the code in the process!
STEP 1: Go to Developer > Record Macro
Go with the default name Macro1 and click OK.
STEP 2: Now it’s starting to record! Here’s what we want to do:
- Select a Cell (A8 in our example below)
- Click Bold
- Click Italic
- Select the highlight color to be yellow
Go to Developer > Stop Recording
STEP 3: Now let us try out the macro in action.
Select the rest of the cells that we want to apply the actions on.
Go to Developer > Macros
Make sure Macro1 is selected and click Run.
And it’s like magic! The same steps are now applied!
STEP 4: Let us have a look how that magic was done. Go to Developer > Visual Basic
Go to Modules > Module1. You can see the code that does the following: Setting Bold, Italic, and filling the color with Yellow.
If you are a beginner in creating Excel macros, this is gold as you get a VBA lesson at the same time! You can see how it bolded the selection, italicized it, then set the corresponding properties to highlight it with the color yellow.
How to Swap Values Using Macros in Excel
Now how about writing Excel macros from scratch? I will guide you on writing your very first Excel macro!
Have you ever wanted to try to swap values from one cell to another?
Plus you also get to learn some programming along the way, how cool is that?
Here are the 2 values that we want to swap:
I will show you step by step, on how we can swap values!
STEP 1: Go to Developer > Insert > ActiveX Controls > Button
Drag anywhere in the Excel spreadsheet to create your Button!
STEP 2: Right click on your new button and select View Code.
STEP 3: Type in this code:
Dim container As Double
container = Range(“A8”).Value
Range(“A8”).Value = Range(“B8”).Value
Range(“B8”).Value = container
Let us go through each line step by step to understand the essence of the code:
Dim container As Double
This creates a new variable named container wherein we can store a numerical value inside. The double type means it can have a numerical value. There are a lot of data types that you can read more about here.
container = Range(“A8”).Value
Now we are getting the first value in Cell A8 and storing it into our container variable.
Range(“A8”).Value = Range(“B8”).Value
We will now copy the content of Cell B8 and paste it into Cell A8. First step done! So far both will have the value 500 in our example.
Range(“B8”).Value = container
Now the last step! Copy the content of the container variable into Cell B8. The swap is now complete! Pretty straightforward right?
STEP 4: Time to test it out! Make sure Design Mode is deselected.
STEP 5: Click on the button now and you will see the values get swapped!








Top 10 Tutorials
- 101 Excel Macros Examples– Do you repeat various Excel actions like copy & pasting data, formatting, hiding and unhiding worksheets, to name a few? Did you know that you can automate your tasks in Excel with a couple of mouse clicks?Read more
- How to Create Your First Macro Button in Excel– If you keep on repeating the same thing over and over again, creating an Excel Macro is perfect for you! It saves you time so you can do things that you like doing, like going home early!Read more
- How to Insert Multiple Rows Using Macros in Excel– Did you know you could insert multiple rows using Macros in Excel? With just a loop and providing a number, you can do this in a single click!Read more
- How to Swap Values Using Macros in Excel– Have you ever wanted to try to swap values from one cell to another? It is possible using Macros in Excel! Plus you also get to learn some programming along the way, how cool is that?Read more
- How to Autofit Columns Using Macros in Excel– My column widths are all over the place and I want to make it look more presentable. Did you know you can autofit columns using Macros in Excel?Read more
- How to Add a Macro to the Quick Access Toolbar Ribbon in Excel– What if there’s a macro that you’re using all of the time, day in, day out? We can add a macro to the Quick Access Toolbar Ribbon in Excel for easy access!Read more
- How to Insert Multiple Columns Using Macros in Excel– Ever wanted to try to insert multiple columns just by typing in a number? It is possible using Macros in Excel!Read more
- Use Calculator By Using Macros In Excel– Inside Excel, there is that cool functionality of launching the calculator from there, and you can do that with Excel Macros! You can now perform some quick calculations using the calculator.Read more
- Free Excel Macros & VBA Webinar Training– Learn How To Record MACROS & Write VBA Code within 1 HOUR!Read more
- How to Autofit Rows Using Macros in Excel– My row heights are all over the place and I want to make it look more presentable. Instead of adjusting them one by one, did you know you can autofit rows using Macros in Excel?Read more
Latest Tutorials