101 Ready To Use Excel Macros
101 Ready To Use Excel Macros
SPECIAL SALES
For more information about buying this eBook in bulk quantities, or
for special sales opportunities (which may include custom cover
designs, and content particular to your business or training goals),
please send us an email to [email protected]
Click on this $1 Trial link to get access to this special reader offer!
CONNECT WITH US
Website, blog & podcast: https://www.myexcelonline.com/
Email: [email protected]
2
AUTHOR BIOGRAPHY
John is currently living in the North of Spain, is married and has two
beautiful kids. John holds a bachelor’s degree in Commerce (Major
in Accounting) and speaks English/Australian, Greek and Spanish.
3
TABLE OF CONTENTS
COPYRIGHT .......................................................................................................... 2
SPECIAL SALES..................................................................................................... 2
MYEXCELONLINE ACADEMY COURSE ........................................................ 2
CONNECT WITH US ............................................................................................ 2
AUTHOR BIOGRAPHY ....................................................................................... 3
TABLE OF CONTENTS ....................................................................................... 4
GETTING STARTED ............................................................................................ 9
How to Use This E-Book ..........................................................................................10
Why VBA? ................................................................................................................11
How to Use Macros .................................................................................................12
Enabling VBA in Excel..............................................................................................20
Insert Button to Run a Macro ...................................................................................22
4
CHART MACROS .............................................................................................. 76
Add Chart Title .......................................................................................................77
Change Chart Type .................................................................................................83
Convert Chart into Image ........................................................................................87
Resize All Charts .....................................................................................................91
5
Remove Apostrophe from Numbers.......................................................................216
Remove Characters at the Start .............................................................................220
Remove Date from Date and Time .........................................................................226
Remove Time from Date and Time ........................................................................230
Remove Decimals from Numbers ..........................................................................234
Replace Blanks with Zeros .....................................................................................237
Trim Extra Spaces from Selection ..........................................................................242
6
PRINTING MACROS ....................................................................................... 351
Print All Comments of a Worksheet .......................................................................352
Print the Selected Area ..........................................................................................356
Print with a Narrow Margin ....................................................................................359
7
ADVANCED MACROS ................................................................................... 447
Convert Selected Range into an Image...................................................................448
Convert Text into Speech ......................................................................................451
Create Table of Contents ......................................................................................454
Excel to Powerpoint ..............................................................................................460
Insert a Linked Image ............................................................................................466
Insert a Row After Each Row ..................................................................................470
Save Selected Range as PDF...................................................................................475
Use Goal Seek .......................................................................................................479
Use the Data Entry Form .......................................................................................484
8
Go to Table of Contents
GETTING STARTED
How to Use This E-Book .............................................................. 10
Why VBA? ................................................................................... 11
How to Use Macros..................................................................... 12
Enabling VBA in Excel ................................................................. 20
Insert Button to Run a Macro ...................................................... 22
9 GETTING STARTED
Go back to Getting Started Go to Table of Contents
There are 101 ready-to-use Macros in this eBook for you to become
more efficient at Excel!
To get the most value out of this eBook, we recommend that you
follow our easy to use step by step guide in each Macro.
Each Macro tutorial has 2 workbooks which you have access to:
1. “No Code” - You need to copy & paste the VBA code provided
in this eBook to the VBA Editor and Run the Macro; and
2. “With Code” –You need to simply Run the Macro.
Here is the download link that has all the workbooks covered in
this eBook.
Why VBA?
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?
VBA stands for Visual Basic for Applications. Visual Basic (VB) is a
programming language from Microsoft used to create your own
custom applications.
Not satisfied with the coverage of Excel functions? You can create
your own Excel user-defined function using VBA. You can then
repeatedly use your user-defined function in the spreadsheet.
The 101 Macros in this book simply need to be copied to the VBA
Editor and you will be able to use them immediately. When the
Macro can be further modified to fit your needs or to expand its
capabilities, we will explain how to in this guide.
If this is your first time using a Macro, read the introductory section
here so that you will be more comfortable with the various Macro
lingo.
Variables:
Loops:
Code Comments:
This line is ignored in the code and is used to “document” the code
so that it is easier for you/others to understand what the code does.
Before using any of the Macros in your Excel files, a best practice is
to back up the Excel file first. This is to provide a safety net if data
gets modified in a different way than you expect. You can safely test
the Macro this way with your current data and load the previous file
if unintended changes take place.
Here is a list of common keywords you will see in Macros. You can
always refer back to this list as you go through the examples in the
book:
Keyword Definition
as Used when defining the data type of a variable
dim Used for declaring variables
Combined with the For keyword (e.g. “for each…”) to access the individual components in
each a collection
else Combined with the then keyword for alternate scenarios
end Used to end a procedure
exit Used to leave a procedure prior to the end statement
for Used to iterate one or more actions a specific number of times
function Defines a block of code that can return a value
if Used for specifying conditions
integer Used to define a number between -32,768 and 32,767
is Compares two object references
long Used to define a number between -2,147,483,648 and 2,147,486,647
next Used with the For keyword to create set of repetitive instructions
on error Used to capture and handle errors properly
resume Used with the On Error keyword to handle errors properly
string Used to define text variables
sub Defines a block of code that does not return a value
then Combined with the If keyword for alternate scenarios
to Used with the For keyword when repeating
with Used to perform multiple operations on a single object
Running a Macro
• This Workbook; or
• All Open Workbooks
Click Run.
Under the Choose commands from drop down, make sure to select
Macros.
Click on the Macro icon that is now located on the top or bottom of
your Ribbon and it will now run this Macro!
After that you should be able to see the Developer tab enabled:
.XLSX vs .XLSM
To ensure all Macros in this book will run without any issues, go to
File > Options > Trust Center > Trust Center Settings > Macro
Settings
The Macro will autofit all of the columns to fit to its contents.
You can use this technique to create buttons to run any Macro.
Final Result:
Exercise Workbook:
STEP 2: Place the shape anywhere on the sheet that you want.
Double click on the shape to type the text: Autofit All Columns.
You can change the font, font size, and center the text as well.
Do not worry as we will discuss the Auto Fit All Columns Macro later
in the book.
Let us try it out now! Click on your shape/button and see the magic
happen! All of your columns are now autofitted!
BASIC MACROS
Add Custom Footer .................................................................... 27
Add Custom Header ................................................................... 31
Add Date In Footer ..................................................................... 35
Add Date In Header .................................................................... 39
How to Autofit Columns ............................................................. 43
How to Autofit Rows................................................................... 47
How to Insert Multiple Columns ................................................. 51
How to Insert Multiple Rows ....................................................... 56
How to Insert Numbers .............................................................. 61
How to Remove Text Wrap ......................................................... 66
Unmerge Cells............................................................................ 70
Use Calcuator ............................................................................. 73
26 BASIC MACROS
Go back to Basic Macros Go to Table of Contents
Final Result:
Exercise Workbook:
Ever wanted to add a footer to your Excel spreadsheet? You can add
a custom footer using Excel Macros!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Final Result:
Exercise Workbook:
Ever wanted to add a header to your Excel spreadsheet? You can add
a custom header using Excel Macros!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
End Sub
Final Result:
Exercise Workbook:
Adding the current date in the footer is very easy using Excel
Macros! You can customize the text even as you see fit if prefer
something else instead of the current date.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Now you should be able to see the current date on your footer!
End Sub
Final Result:
Exercise Workbook:
Did you know that you can add the current date in header in Excel?
You can add this via Excel Macros in a single click!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Now you should be able to see the current date on your header!
Final Result:
Exercise Workbook:
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?
And with this cool trick, it can be done in just one click!
Paste in your code and Select Save. Close the window afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the columns are automatically fitted now!
Final Result:
Exercise Workbook:
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?
And with this cool trick, it can be done in just one click!
Paste in your code and Select Save. Close the window afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the rows are automatically fitted now!
Final Result:
Exercise Workbook:
Plus you also get to learn some programming tricks along the way,
how cool is that?
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Select any cell that you want to insert columns on. Go to Developer
> Code > Macros
With that, you are now able to insert multiple columns using
Macros!
Final Result:
Exercise Workbook:
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!
Select any cell that you want to insert rows on. Go to Developer >
Code > Macros
With that, you are now able to insert multiple rows using Macros!
Asks for a max number, then generates numbers from 1 to the max
number
Final Result:
Exercise Workbook:
Select any cell that you want to insert numbers on. Go to Developer
> Code > Macros
With that, you are now able to insert consecutive numbers using
Macros!
Removes text wrap in all cells and then autofit all of the cells
Final Result:
Exercise Workbook:
Ever wanted to remove all the text wrapping, but the hassle of your
cells spilling over is stopping you? Have no fear, Macros are here to
save the day! You can remove text wrap using Macros in
Excel then autofit the cells as well!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the cells now have text wrap removed!
Unmerge Cells
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Make sure you have selected your merged cells for unmerging. Go
to Developer > Code > Macros
With just one click, all of the cells are now unmerged!
Use Calculator
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
With just one click, you have launched the calculator inside Excel!
CHART MACROS
Add Chart Title........................................................................... 77
Change Chart Type..................................................................... 83
Convert Chart into Image ........................................................... 87
Resize All Charts ........................................................................ 91
76 CHART MACROS
Go back to Chart Macros Go to Table of Contents
End Sub
Final Result:
Exercise Workbook:
Did you know that you can programmatically make changes to Excel
Charts? Yes you can! Let us try updating a chart by adding the chart
title using Excel Macros!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the chart. Make sure your chart is
selected. Go to Developer > Code > Macros
Type in the title you want for your chart. Click OK.
End Sub
Final Result:
Exercise Workbook:
You can change the chart type in the code to the type you prefer.
The list of chart types are listed here.
Open the sheet containing the chart. Make sure your chart is
selected. Go to Developer > Code > Macros
ActiveChart.ChartArea.Copy
ActiveSheet.Range("A1").Select
'Converts the Chart into an Image
'Then it gets pasted on Cell A1 of the active sheet
ActiveSheet.Pictures.Paste.Select
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the chart. Make sure your chart is
selected. Go to Developer > Code > Macros
With just one click, your chart is now converted into an image!
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. You can change the
Height and Weight if you prefer a different size than 400. Close
the window afterwards.
Open the sheet containing the charts. Go to Developer > Code >
Macros
With just one click, all of your charts are now resized!
FORMULA MACROS
Add a Degree Symbol to Selection .............................................. 98
Add letters A-Z ......................................................................... 102
Calculate Square Root .............................................................. 106
Convert Date into Day .............................................................. 110
Convert Date into Month .......................................................... 115
Convert Date into Year ............................................................. 120
Convert Formulas into Values ................................................... 125
Convert Negative Numbers to Positive ...................................... 130
Convert Roman Numbers into Modern Numbers ....................... 135
Convert Selection to Lower Case ............................................... 140
Convert Selection to Proper Case .............................................. 144
Convert Selection to Sentence Case .......................................... 148
Convert Selection to Upper Case............................................... 153
Convert Time into Hour ............................................................ 157
Convert Time into Minutes ....................................................... 161
Convert Time into Seconds ....................................................... 165
Convert UK Dates to US Dates .................................................. 169
Convert US Dates to UK Dates .................................................. 173
Extract Number from Text ........................................................ 177
Format Values to Dollars and 2 Decimal Places ......................... 182
Get Word Count from Worksheet .............................................. 186
Increase Cell Values by a Number ............................................. 191
Decrease Cell Values by a Number............................................ 196
Insert Time Range .................................................................... 201
96 FORMULA MACROS
Go to Table of Contents
97 FORMULA MACROS
Go back to Formula Macros Go to Table of Contents
Final Result:
Exercise Workbook:
Have numbers that are degrees but you are having difficulty
inserting the degree symbol to each? Excel Macros can modify your
numbers and you will add a degree symbol to your selection in a
single click!
You will learn here on how you can customize text and modify it
using Macros.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all your numbers have the degree symbol
added to it!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
With just one click, all of the letters A-Z are added in a single
click!
Final Result:
Exercise Workbook:
Here are the numbers that we want to get the square root of:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your numbers are
highlighted. Go to Developer > Code > Macros
With just one click, you have calculated the square root of all
your numbers!
End Sub
Final Result:
Exercise Workbook:
Macros are also capable of processing dates and times. Let us see
how we can convert date into day values using Excel Macros!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of your dates are now converted to days!
End Sub
Final Result:
Exercise Workbook:
Macros are also capable of processing dates and times. Let us see
how we can convert date into month values using Excel Macros!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of your dates are now converted to
months!
End Sub
Final Result:
Exercise Workbook:
Macros are also capable of processing dates and times. Let us see
how we can convert date into year values using Excel Macros!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of your dates are now converted to years!
End Sub
Final Result:
Exercise Workbook:
That's right, Excel Macros can convert formulas into values in one
step!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your formula cells
are highlighted. Go to Developer > Code > Macros
With just one click, all of the formula cells are now converted to
values! The values also changed because of the nature of the
RANDBETWEEN formula, as it changes along with any change done
to the workbook.
Selection.Value = Selection.Value
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your negative
numbers are highlighted. Go to Developer > Code > Macros
With just one click, all of your numbers are now converted to
positive!
Selection.Value = Selection.Value
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your Roman
Numbers are highlighted. Go to Developer > Code > Macros
With just one click, all of the Roman Numbers are now converted
correctly!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of the cells now converted to lower case!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of the cells now converted to proper case!
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of the cells now converted to sentence
case!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of the cells now converted to upper case!
Final Result:
Exercise Workbook:
Macros are also capable of processing dates and times. Let us see
how we can convert time into hour values using Excel Macros!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of your times are now converted to hours!
Final Result:
Exercise Workbook:
Macros are also capable of processing dates and times. Let us see
how we can convert time into minute values using Excel Macros!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of your times are now converted to
minutes!
Final Result:
Exercise Workbook:
Macros are also capable of processing dates and times. Let us see
how we can convert time into second values using Excel Macros!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of your times are now converted to
seconds!
Selection.NumberFormat = "MMM-DD-YY"
End Sub
Final Result:
Exercise Workbook:
Do you recall having a scenario where your dates in Excel are using
the incorrect format? You can convert UK dates to US dates using
Macros in Excel!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of your dates are now in the US format!
Selection.NumberFormat = "DD-MMM-YY"
End Sub
Final Result:
Exercise Workbook:
Do you recall having a scenario where your dates in Excel are using
the incorrect format? You can convert US dates to UK dates using
Macros in Excel!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of your dates are now in the UK format!
Creates a custom formula that extracts the numeric part from text
LengthOfText = Len(CellRef)
GetNumericPart = Result
End Function
Final Result:
Exercise Workbook:
Want to create your own formula definition? You can do that using
Excel Macros! Let us create a formula that extracts the number
from text. The assumption here is there is only one number in your
text to extract.
Paste in your code and Select Save. Close the window afterwards.
Open the sheet containing the data. Type in your new custom
formula: =GetNumericPart(B8)
Do the same for the rest of the cells. And you have extracted the
numeric part using Macros!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of your numbers are now formatted as
dollars with 2 decimal places!
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, you now have displayed your word count!
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your values are
highlighted. Go to Developer > Code > Macros
With just one click, all of the values are now added by 5!
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your values are
highlighted. Go to Developer > Code > Macros
With just one click, all of the values are now subtracted by 5!
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your values are
highlighted. Go to Developer > Code > Macros
With just one click, all of the values are now multiplied by 5!
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your id numbers are
highlighted. Go to Developer > Code > Macros
With just one click, you have removed all of the dashes!
Selection.Value = Selection.Value
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your numbers are
highlighted. Go to Developer > Code > Macros
With just one click, all of the apostrophes are now removed!
End Sub
Final Result:
Exercise Workbook:
This is the text that we want to modify. We want to remove the first
2 characters of these Part Numbers:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your part numbers
are highlighted. Go to Developer > Code > Macros
With just one click, the first 2 characters are now all removed!
Final Result:
Exercise Workbook:
Have a bunch of date times, and you want to keep the time only?
You can remove date from the date and time using Excel Macros
in a single click!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your date times are
highlighted. Go to Developer > Code > Macros
With just one click, all of the dates from the date times are now
removed!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your date times are
highlighted. Go to Developer > Code > Macros
With just one click, all of the times from the dates are now
removed!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your numbers are
highlighted. Go to Developer > Code > Macros
With just one click, all of the decimal components are now
removed!
Selection.Value= Selection.Value
End Sub
Final Result:
Exercise Workbook:
This is our list of values, see that the Sales column has blanks:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your sales column is
highlighted. Go to Developer > Code > Macros
With just one click, all of the blanks are now replaced with zeros!
Final Result:
Exercise Workbook:
If you want to quickly remove extra spaces at the start and end of
your text, Excel Macros will do that for you! It is very similar to the
TRIM formula as well.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your text is
highlighted. Go to Developer > Code > Macros
With just one click, all of the unneeded extra spaces are all
removed!
HIGHLIGHTING MACROS
Highlight Active Row & Column ................................................ 247
Highlight All Errors with a Red Color ........................................ 251
Highlight All Named Ranges ..................................................... 255
Highlight Alternate Rows in Selection ....................................... 259
Highlight and Count a Specified Value ...................................... 263
Highlight Cells with a Single Space ........................................... 269
Highlight Cells with Comments ................................................ 274
Highlight Custom Text ............................................................. 278
Highlight Duplicates ................................................................. 283
Highlight Unique Values in Selection ........................................ 288
Highlight Max Value in Selection .............................................. 291
Highlight Min Value in Selection ............................................... 296
Highlight Negative Values ........................................................ 300
Highlight Top 10 Values of Selection ........................................ 305
Highlight Bottom 10 Values of Selection ................................... 310
Highlight Values Greater Than .................................................. 315
Highlight Values Lesser Than ................................................... 321
Spell check and Highlight Misspellings ..................................... 327
Final Result:
Exercise Workbook:
We have a cool trick to show you using Macros, you can highlight
the active row and column by double clicking!
Do take note that this will override your original double click
behavior.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Just double click anywhere, and it will highlight the active row and
column!
Final Result:
Exercise Workbook:
Here is our initial set of data, you can see there are a lot of errors in
there:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the cells with errors are now
highlighted!
Final Result:
Exercise Workbook:
If you have a lot of named ranges in your worksheet, and you want
to see which ones are there quickly? You can quickly highlight all
named ranges using Excel Macros!
For our example, these columns over here have named ranges
defined.
Here are the named ranges, you can quickly view them via Formulas
> Defined Names > Name Manager:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the named ranges are now highlighted!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, the alternate rows of your selection are now
highlighted!
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
Type in the value to highlight and count. Let us type in Ian Wright.
Click OK.
With just one click, all of the Ian Wright values are now
highlighted and you see the count as well!
End Sub
Final Result:
Exercise Workbook:
This is our initial set of data, the ones enclosed in red are the ones
with a single space (although it is not visible at an initial glance).
Double click in the empty cells and press CTRL + A which will
highlight the single space.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the cells with a single space are now
highlighted!
Final Result:
Exercise Workbook:
This is our initial set of data, you can see the ones with the red
triangle on the right corner are the ones with comments inserted:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the cells with comments are now
highlighted!
'If the value is the same as the custom text, then set
the font color to red
If rng.Value = txt Then
rng.Font.Color = RGB(255, 0, 0)
End If
Next
End Sub
Final Result:
Exercise Workbook:
Excel has search text functionality, however we can also use Excel
Macros to highlight custom text! That's right, you can use it to
highlight any text that you specify!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all Ian Wright values are now highlighted!
Highlight Duplicates
End Sub
Final Result:
Exercise Workbook:
Want to learn how to highlight cells that are duplicates and learn
Macros at the same time? Let me show you how to highlight
duplicates using Macros in Excel!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
STEP 3: Let us test it out! Make sure to have your cells selected.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the duplicate cells now have been
highlighted!
Final Result:
Exercise Workbook:
Want to find the values in your list that are unique? It is very easy to
do with Excel Macros, you can highlight unique values in your
selected range in a click!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of the unique values are now highlighted!
End Sub
Final Result:
Exercise Workbook:
Have a lot of values and want to find the largest value easily? Excel
Macro will do the heavy lifting for you by highlighting the max
value in your selection of values!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, your cell with the largest value is now
highlighted!
Final Result:
Exercise Workbook:
These are our set of values, let us go find the smallest one!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, your cell with the smallest value is now
highlighted!
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With just one click, all of the negative values are now marked as
color red!
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.C
ount).SetFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Top
.Rank = 10
.Percent = False
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
STEP 3: Let us test it out! Select the data that you want its top 10
values to be highlighted.
With just one click, your top 10 values are now highlighted!
Selection.FormatConditions.AddTop10
Selection.FormatConditions(Selection.FormatConditions.C
ount).SetFirstPriority
With Selection.FormatConditions(1)
.TopBottom = xlTop10Bottom
.Rank = 10
.Percent = False
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
STEP 3: Let us test it out! Select the data that you want its bottom
10 values to be highlighted.
With just one click, your bottom 10 values are now highlighted!
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue,
Operator:=xlGreater, Formula1:=value
Selection.FormatConditions(Selection.FormatConditions.C
ount).SetFirstPriority
End Sub
Final Result:
Exercise Workbook:
You will be surprised on how much you can do with Macros. It can
highlight values in your selected range that are greater than a
value you specify.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
Enter the greater than value, we will enter 50000. Click OK.
With just one click, all of the values greater than 50,000 are now
highlighted!
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue,
Operator:=xlLess, Formula1:=value
Selection.FormatConditions(Selection.FormatConditions.C
ount).SetFirstPriority
Final Result:
Exercise Workbook:
Want to try something new with Macros? You can highlight values
in your selected range that are lesser than a value you specify.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
Enter the lesser than value, we will enter 50000. Click OK.
With just one click, all of the values lesser than 50,000 are now
highlighted!
Final Result:
Exercise Workbook:
Excel has spell checking functionality, and we can take this to the
next level by using Excel Macros to spell check then highlight
misspellings for you!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the cells with incorrect spellings are
now highlighted!
Sub DisableGetPivotData()
Application.GenerateGetPivotData = False
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. This will create two
options for you to choose either to enable or disable. Close the
window afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
Try referencing a cell inside the pivot table again. It is now a normal
cell reference.
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your pivot table is
selected. Go to Developer > Code > Macros
With just one click, your Pivot Table subtotals are now hidden!
End Sub
Final Result:
Exercise Workbook:
These are the two pivot tables using this data source:
Now let's make change to have one big sales value! Let us see if the
pivot tables will reflect these values.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of your pivot tables are now refreshed!
End Sub
Final Result:
Exercise Workbook:
To demonstrate, let us try to refresh all pivot tables and see if the
columns get autofitted. Go to Data > Connections > Refresh All
You can see our columns got autofitted (Column E stands out).
Let us turn this off with Excel Macros! Now press the undo button
to undo the change we made.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
Now let us refresh all the pivot tables. Go to Data > Connections >
Refresh All
All of your pivot tables are now refreshed and have kept their
original column widths!
PRINTING MACROS
Print All Comments of a Worksheet .......................................... 352
Print the Selected Area ............................................................. 356
Print with a Narrow Margin ....................................................... 359
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
Scroll to the last page. You can now see the following information
for the comments: Cell location, comment and the author of the
comment!
End Sub
Final Result:
Exercise Workbook:
Did you know that you could print out a specific area in your
spreadsheet? Yes that's right, you can use Excel Macro to just print
the selected area you have chosen! You get to save some printer ink
in the process as well!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
STEP 3: Let us test it out! Select the area that you only want to
print.
Open the sheet containing the data. Make sure your data is
highlighted. Go to Developer > Code > Macros
With this, you have printed out the selected area only!
Prints out with narrow margins then shows the print dialog
Application.Dialogs(xlDialogPrint).Show
End Sub
Final Result:
Exercise Workbook:
In Excel we can opt to print out with very narrow margins through
its interface. We can do the same thing with Excel Macros, you can
print with a narrow margin, or even customize the margin values
so that it will be constant every time!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, you have now printed out with narrow
margins!
WORKBOOK MACROS
Attach Current Workbook into an Email Message ...................... 366
Close All Workbooks and Save Changes .................................... 371
Copy Current Worksheet into a New Workbook ......................... 375
Create a Backup ....................................................................... 379
Get the Count of Unsaved Workbooks ...................................... 383
Set the Active Worksheet .......................................................... 388
Show a Closing Message .......................................................... 391
Show a Welcome Message ........................................................ 394
End Sub
Final Result:
Exercise Workbook:
You can change the following fields - To, Subject and Body
depending on your preferences. These are marked in yellow.
With just one click, your Excel Workbook is now attached to the
email message!
Final Result:
Exercise Workbook:
Paste in your code and Select Save. Close the window afterwards.
With just one click, all of your workbooks are now saved and
closed!
Final Result:
Exercise Workbook:
Have a worksheet that you want to copy into a new worksheet? You
can accomplish that with a single line of Excel Macro code! It will
copy the current worksheet into a new workbook.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data, this is the one we want to copy
to a new worksheet.
With just one click, your active worksheet was copied to a new
workbook!
Create a Backup
End Sub
Final Result:
Exercise Workbook:
Make sure to change the directory into a folder that exists for you:
STEP 3: Let us test it out! You can see that we have no files yet in
the directory:
With just one click, you have generated a backup of your current
workbook!
MsgBox "You have " & counter & " unsaved workbook(s)"
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
With just one click, you now have the number of unsaved
workbooks!
Final Result:
Exercise Workbook:
Want to try something cool when your users open your workbook?
Set a default worksheet to open using Excel Macros!
STEP 2: Paste in your code and Select Save. Make sure it is saved
in ThisWorkbook.
You can change the Sheet Name in the code (e.g. "Europe") if you
want a different default worksheet.
STEP 3: Let us test it out! Close your workbook. Then open it up.
Your Macro will execute once you open the workbook. Now you can
see your new default worksheet!
End Sub
Final Result:
Exercise Workbook:
We have a cool trick to try out using Excel Macros, we can show a
closing message to thank your user once they close the
spreadsheet.
STEP 2: Paste in your code and Select Save. Make sure it is saved
in ThisWorkbook.
You can change the text to show in the closing message as well.
Your Macro will execute once you close the workbook. Now you can
see your closing message!
End Sub
Final Result:
Exercise Workbook:
Want to try something cool when your users open your workbook?
Show a welcome message using Excel Macros!
STEP 2: Paste in your code and Select Save. Make sure it is saved
in ThisWorkbook.
You can change the text to show in the welcome message as well.
STEP 3: Let us test it out! Close your workbook. Then open it up:
Your Macro will execute once you open the workbook. Now you can
see your welcome message!
WORKSHEET MACROS
Delete All Other Worksheets ..................................................... 398
Delete Blank Worksheets .......................................................... 401
Hide All Other Worksheets........................................................ 405
Insert Multiple Worksheets ....................................................... 409
Protect Active Worksheet .......................................................... 414
Unprotect Active Worksheet ..................................................... 418
Protect All Cells With Formulas ................................................. 422
Protect All Worksheets ............................................................. 426
Save Each Worksheet as a PDF File ............................................ 432
Sort All Worksheets Alphabetically ............................................ 436
Unhide All Hidden Rows and Columns ...................................... 439
Unhide All Worksheets ............................................................. 443
Next worksheet
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
With just one click, all of the other worksheets are now deleted!
Final Result:
Exercise Workbook:
These are our worksheets, the ones enclosed in red are blank
worksheets:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
With just one click, all of the blank worksheets are now deleted!
End Sub
Final Result:
Exercise Workbook:
Want to hide all worksheets except your active one? You can hide all
other worksheets using Macros in Excel!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
STEP 3: Let us test it out! These are all of our worksheets that we
want to hide.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the other worksheets are all hidden!
End Sub
Final Result:
Exercise Workbook:
We only have one worksheet so far, let us work out our magic!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Try editing your worksheet. You can verify that the worksheet is
now protected!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Now your sheet is now unprotected! We can now try editing the
sheet.
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the cells with formulas are now
protected!
End Sub
Final Result:
Exercise Workbook:
You can protect all worksheets using Macros in Excel! You will ask
the user for the password then use it to protect all of the
worksheets in the file.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
With just one click, all of your worksheets are now protected!
End Sub
Final Result:
Exercise Workbook:
Want to export and save each worksheet as a PDF file? You can
do that with a single click using Excel Macros!
Open the sheet containing the data. Go to Developer > Code >
Macros
Let us check the folder, and you will see these worksheets:
Let us open one of the files, and it is exactly the same as the one in
the workbook!
Dim i As Integer
Dim j As Integer
'We use two loops to sort the sheets in ascending order
For i = 1 To Sheets.Count
For j = 1 To Sheets.Count - 1
If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name)
Then
Sheets(j).Move After:=Sheets(j + 1)
End If
Next j
Next i
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the worksheets are now sorted
alphabetically!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the hidden rows and columns are now
shown!
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
STEP 3: Let us test it out! This is the only tab we are seeing at the
moment.
Open the sheet containing the data. Go to Developer > Code >
Macros
With just one click, all of the hidden worksheets are now shown!
ADVANCED MACROS
Convert Selected Range into an Image ...................................... 448
Convert Text into Speech ......................................................... 451
Create Table of Contents .......................................................... 454
Excel to Powerpoint .................................................................. 460
Insert a Linked Image ............................................................... 466
Insert a Row After Each Row ..................................................... 470
Save Selected Range as PDF ...................................................... 475
Use Goal Seek .......................................................................... 479
Use the Data Entry Form ........................................................... 484
Selection.Copy
'Paste the selection as an image
ActiveSheet.Pictures.Paste.Select
End Sub
Final Result:
Exercise Workbook:
Did you know that even Excel Macros you can create screenshots
with it? That's right, let us use Excel Macros to convert your
selected range into an image!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the header. Make sure the header is
highlighted. Go to Developer > Code > Macros
With just one click, you have now created a screenshot using
Macros! You can even rotate it or resize it now.
Final Result:
Exercise Workbook:
Did you know that Excel can actually talk? That's right, we can use
Excel Macros to convert text into speech. We can instruct Excel to
read text aloud to us!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your sentences are
highlighted. Go to Developer > Code > Macros
With just one click, you can hear your text being read aloud! Make
sure your sounds are turned on
Next counter
End Sub
Final Result:
Exercise Workbook:
This is one of the most fun and coolest Macros that you can use. If
you have a lot of worksheets, it is very annoying to scroll left to
right to find out what other worksheets you have. We can use Excel
Macros to create a table of contents for easy navigation!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Try clicking the Americas link and it takes you straight to the
Americas worksheet!
Excel to Powerpoint
pptApp.Visible = True
pptApp.Activate
Application.CutCopyMode = False
End Sub
Final Result:
Exercise Workbook:
Did you know that you can use Excel Macros to copy anything from
your spreadsheet into a Powerpoint presentation? Yes you can!
Look for Microsoft PowerPoint 16.0 Object Library and tick it.
Click OK.
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
With just one click, you now have your selected range copied into
Powerpoint!
Selection.Copy
'Paste the selection as an image
ActiveSheet.Pictures.Paste(Link:=True).Select
End Sub
Final Result:
Exercise Workbook:
Did you know that you can create a linked image in Excel?
(You can do this on any part of the spreadsheet, not just logos!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing our target. Make sure the logo cells are
highlighted. Go to Developer > Code > Macros
With just one click, you have created a linked image! Double click
on it and it will highlight the logo!
rowCnt= Selection.EntireRow.Count
End Sub
Final Result:
Exercise Workbook:
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure your table is
highlighted. Go to Developer > Code > Macros
With just one click, you have now inserted blank rows after each
row!
End Sub
Final Result:
Exercise Workbook:
Excel Macros are incredibly flexible and you will be surprised with
its bag of tricks! One of them is you can save any selected range
as PDF. That's right, any section in the spreadsheet that you have
selected gets saved into a PDF!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
With just one click, you now have your selected range saved into
a PDF!
Final Result:
Exercise Workbook:
Ever heard of the Goal Seek feature in Excel? It is a very nice feature
where it takes out the guesswork for you and determines the input
value needed to achieve a specific goal. For example, you have a
goal / result in mind, but you are unsure what the starting amount
should be for an investment. Let us use Excel Macros to execute the
Goal Seek feature!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Do take note that we are referencing these two cells in the code:
What goal seek will do, is it will adjust the initial amount (Cell A9),
to achieve the target goal that you specify (Cell E9) which is
$250,000.
Open the sheet containing the data. Go to Developer > Code >
Macros
Type in the target value of $250,000. See how the initial amount will
change.
With just one click, Goal Seek computed that you need an initial
amount of $68,921.35 to achieve your goal of $250,000!
Loads the data form to allow you to populate data to the table
Final Result:
Exercise Workbook:
Have a lot of details to encode in your table? You can use Excel
Macros to open the data entry form for you with one click!
STEP 2: Paste in your code and Select Save. Close the window
afterwards.
Open the sheet containing the data. Make sure the correct sheet is
selected. Go to Developer > Code > Macros
With just one click, you now have the data form loaded up! Let us
try populating a couple of records then click Close.