How to Create a Custom Excel Add-in (Step-by-Step Guide)
š Learn Excel Automation with Python: https://yt.pythonandvba.com/avdVI14AxzM/excel-automation-course/
ššš¦šš„šš£š§šš¢š”
āāāāāāāāāāāāāāāāāāāāāāāāāā
Supercharge your Excel skills and productivity by learning how to create a custom Excel add-in using VBA (Visual Basic for Applications) in this easy-to-follow, step-by-step tutorial. Perfect for both beginners and advanced users, this guide will walk you through the entire process of creating your very own Excel add-in.
VBA allows you to quickly and efficiently build custom add-ins that can expand Excel's capabilities, automate repetitive tasks, and integrate with external tools and services. It's an excellent way to get started with creating and distributing add-ins due to its user-friendliness and ease of implementation. While there are other methods, such as COM, VSTO, and Office.JS, our focus will be on VBA in this tutorial.
By the end of this video, you'll have a custom ribbon that allows you to run any macro with ease, taking your Excel abilities to new heights!
š ššš”šš¦:
ā¶ GitHub Repo: https://github.com/Sven-Bo/excel-add-in-tutorial-template
ā¶ Office RibbonX Editor: https://github.com/fernandreu/office-ribbonx-editor/releases/tag/v1.9.0
ā¶ MyToolBelt Add-in: https://yt.pythonandvba.com/avdVI14AxzM/mytoolbelt
ā¶ Office imageMSO icon library: https://bert-toolkit.com/imagemso-list.html
ā¶ Blog Post (XML/VBA Starter Code): https://bettersolutions.com/vba/ribbon/document-level-custom-ui-editor.htm
ā¶ Microsoft Custom UI Docs: https://learn.microsoft.com/en-us/openspecs/office_standards/ms-customui/d842006e-3187-4f66-a17d-0819a3cc94b5
ā š§šš šš¦š§šš š£š¦:
0:00 - Introduction
1:06 - Creating an XLSM File
1:30 - Customizing the Excel Ribbon
5:12 - Integrating ChatGPT
7:45 - Adding More Buttons to the Add-in
10:39 - Exploring Other Custom UI Elements
11:30 - Distributing Your Add-in
13:00 - Outro
š š¢š„š šš„š¢š š š
āāāāāāāāāāāāāāāāāāāāāāāāāā
š§° My Excel add-ins and Windows apps: https://yt.pythonandvba.com/avdVI14AxzM/solutions/
š LinkedIn: https://www.linkedin.com/in/sven-bosau/
š¬ Contact: https://yt.pythonandvba.com/avdVI14AxzM/contact
Closed Caption:
Hey, guys! In today's video, we're going toĀ
create your very own Excel add-in. I'll guide
you through the entire process, step by step.
With a custom add-in, you can expand Excel's
capabilities, automate tasks, and integrateĀ
with external tools and services. Additionally,
you can share your add-in withĀ
colleagues or even sell it.
By the end of this tutorial, you'll have a customĀ
ribbon that lets you run any macro you want.
As an example, we'll integrate ChatGPTĀ
right into Excel. With just one click,
you'll be able to get the ChatGPTĀ
response in a new worksheet. Plus,
I'll demonstrate how you can further develop thisĀ
add-in by adding even more features and buttons.
Now, there are several ways to create an add-in,Ā
like VBA, VSTO, COM add-ins, and Office JS. I
might make a separate video discussing theĀ
advantages and disadvantages of each method. But
for this tutorial, we'll focus on the VBA optionĀ
because, in my opinion, it's the easiest method.
Alright, so without further ado, let'sĀ
dive in and create your very own add-in.
To kick things off, open a standard ExcelĀ
file. It's totally fine if the file is
empty. Our main goal here is to save it asĀ
a macro-enabled workbook. To do this, head
over to File > Save As > and then select > ExcelĀ
Macro-Enabled Workbook. Great, with that done,
let me hop back to my folder and delete theĀ
.xlsx file, as we won't be needing it anymore.
To create our add-in, we'll use a fantasticĀ
free software called "Office RibbonX". You can
easily download the tool from GitHub, and don'tĀ
worry; I'll include the links in the description
box of this video. On the GitHub page, simplyĀ
scroll down to the assets section. Since this
tool also requires the ".NET 6" framework, I'dĀ
recommend downloading the following installer.
Once the download wraps up, goĀ
ahead and open the installer.
For the installation, you can stick withĀ
the default settings. If you'd like,
you can also create a desktopĀ
shortcut for easy access.
Now, we'll use this tool to create a custom ribbonĀ
for our Excel file. To do this, click "Open"
and select the macro-enabled workbook we createdĀ
earlier. Next, right-click on the file and choose
"Insert Office 2010+ Custom UI part." Afterwards,Ā
double-click on the XML file and your editor
window should look like mine. In this editor,Ā
we'll insert the XML code. I found a helpful blog
post that explains the steps I just demonstrated.Ā
On this site, you'll also discover some sample XML
code to get started. So, let me copy theseĀ
XML tags and paste them into the editor.
Before moving on, I'll make two minor adjustments.Ā
I'll set "startFromScratch" to false instead of
true, and I'll remove "ThisWorkbook" from theĀ
onAction parameter. I'll go over the other XML
tags shortly, but first, let me validateĀ
the syntax by clicking this button. Great,
the XML is valid, so I can now click theĀ
save button. By doing this, we're injecting
these XML tags into our Excel workbook. ToĀ
verify this, let me reopen our spreadsheet.
You'll see that we now have a new ribbonĀ
called MyTab, and inside the ribbon, there's
a large button. This is due to our XML code.
XML uses tags. We created the tab using the
"tab" tag, where we can specify an ID and a label.Ā
It's important to ensure that the ID is unique.
The ID is mainly for developers to manipulate orĀ
retrieve information from the element. The label,
on the other hand, is what the user sees in Excel.Ā
In our case, it's "My Tab." Within the tab, we've
created a group, and inside this group, there'sĀ
a button. The button has additional arguments,
such as the image, size, label, screenTip,Ā
and onAction. I'll discuss the image and size
shortly. The screenTip is simply what users seeĀ
when they hover their mouse over the button. The
crucial part for us is the onAction parameter,Ā
where we can define which macro to execute when
the button is clicked. Currently, it's setĀ
to "MyMacro." At the moment, we don't have a
macro with this name, which is why we receiveĀ
an error message when clicking the button.
To resolve this issue, let's create a subroutineĀ
called "MyMacro." To do this, navigate to the
Developer tab. If it's not visible, right-click onĀ
the ribbon, select "Customize the Ribbon," check
the box next to Developer, and click OK. With thatĀ
in place, open the Visual Basic editor and insert
a new module. For our example, I'll use the codeĀ
provided in the blog post. Let me copy the code
and paste it into the module. It's important thatĀ
the subroutine name matches the name specified
in your XML code. To call the macro from theĀ
ribbon, include this line. That's essentially
all there is to it. When I go back and clickĀ
the button, we can see the macro is executed.
Now, if you're watching this video, you mightĀ
already have a few macros you want to include
in your add-in. If not, let's build somethingĀ
useful together. I've created two modules on
my GitHub page. The first one is a VBA codeĀ
to bring ChatGPT into Excel. All you need
to do is click the copy icon, go back to ourĀ
module, and replace the current code with the
one from GitHub. To use this macro, you needĀ
an OpenAI API key. In the following video,
I'll show you how to get one. I already have one,Ā
so let me paste it in here. I want to execute this
macro when clicking a button in my ribbon, soĀ
I've included "Control as IRibbonControl." Next,
I'll input the macro name in our XML code.Ā
Therefore, let me save and close the workbook.
Now, let's modify the XML. Instead of "My Tab,"Ā
I'll call it "My Add-in," and the group label will
be "ChatGPT." For the button image, you can useĀ
built-in images from Microsoft. On the following
page, you can find all available images. When IĀ
search for "HappyFace," you'll see the image we
use in our add-in. To replace it, scroll throughĀ
this page and use the name on the left-hand side.
However, you're not limited to built-in images;Ā
you can also use any other images you might have.
To demonstrate this, let me go to "flaticon.com"Ā
and search for an AI-related icon. I'll choose
this one and recommend using 32x32 pixels for theĀ
size. After downloading it, I'll copy the image
from my downloads folder into my project filesĀ
to stay organized. Next, click "Insert Icons" and
select the one we just downloaded. It will appearĀ
on the left-hand side. To use it, remove "Mso" and
use the image name, which in my case is "ai."
Additionally, I'll change the label and
screenTip and ensure the macro name is adjusted toĀ
"OpenAI_Completion." Before saving, let's validate
the format. Excellent, everything looks good,Ā
so we can return to Excel to view the changes.
Now, we can see that the tab name has changed, andĀ
we can also see our new button. To test it out,
let me generate a 2-week travel plan for SriĀ
Lanka. When selecting the cell and clicking
the button, our macro will be executed. As aĀ
result, we'll get the response from ChatGPT in
a new worksheet where I can find my travel plan.Ā
With that working, let's add a few more buttons
to our ribbon. First, I'll insert a new moduleĀ
to keep things organized. In my GitHub repo,
you'll find an additional macro. Let me copy theĀ
code and paste it into the new module. I now have
two more subroutines - IfErrorBlank & IfErrorZero.Ā
In just a moment, I'll explain what those macros
do. But for now, remember that if you want toĀ
call those subroutines from your add-in, you
need to pass in "control as iRibbonControl." WithĀ
that said, let me save and close the workbook.
Back in the Ribbon Editor, I'llĀ
copy and paste the group XML tag.
The ID and label will be Utils. Remember thatĀ
the IDs should be unique. So, instead of Button1,
I'll change it to Button2. For the image, I'llĀ
use a built-in one, so I need to change this to
"imageMso". I am going to use the EqualSign icon.
Additionally, I'll change the button size and
label. By the way, the screenTip is optional,Ā
so you can also remove it if you want.
For the action, I'll again use the subroutineĀ
name, which in my case is IfErrorBlank. Within
the same group, I'll now insert a second buttonĀ
and change the label and the onAction parameter.
In my case, I actually forgot to change theĀ
button ID. So, when I click on validate,
the Ribbon Editor points to the respective lineĀ
and says the ID is already used. Let me change it
to "button3." With that change in place, let meĀ
save the XML code, go back to my project folder,
and reopen our spreadsheet. If I navigate to ourĀ
add-in, we can see our new "Utils" group, which
includes two new buttons. When I click on one ofĀ
those buttons, nothing seems to happen, which is
a good sign, as we don't get any error message.
To demonstrate what those two buttons do, let me
generate some sample sales data by month with theĀ
help of ChatGPT. Once I have the data, I'll split
the text into columns so that we have some data toĀ
play with. Now, imagine you want to use a simple
VLOOKUP on this data. It's pretty straightforward;Ā
I'll do it for January, March, and April.
Yet, when you misspell the month name, the VLOOKUPĀ
returns an error. Now, if you want to use a blank
cell instead of the error, you can use the newĀ
buttons. If you select the cell with the formula
and click on it, the macro will wrap an IFERRORĀ
statement around the formula. This works with
any formula you may have. Let me do the same forĀ
the other cell, but instead of a blank cell, I'd
like to show a 0. Great, this is all working fine.
So far, we've only inserted buttons to trigger our
macros. But you can also use different elements.Ā
To demonstrate this, let me show you an example
of my own add-in I created. If you'd like to grabĀ
the free version of this add-in, I'll leave the
link in the description below. In my add-in, IĀ
used other elements, like this drop-down menu or
the dialogue box launcher. To find all availableĀ
elements and how to use them, I recommend checking
out the official Microsoft documentation. OfĀ
course, I'll include the link in the description
of this video. Here, you can take a look at theĀ
different elements and check out their parameters.
For instance, we've used a button; to implementĀ
it, you need at least an ID, label, and image.
However, the button also has more attributes,Ā
which you can find here. So, definitely check
out this page and see what is possible.
Now, back to our add-in. The last step is
to create the actual add-in file and to show youĀ
how users can install it on their machines. To
do this, let me clean up our workbook by removingĀ
the additional sheet and clearing the first sheet.
Now we can save the file as an Excel add-inĀ
by selecting "Excel add-in" with the file
ending "xlam". In my case, I'll keep it in ourĀ
project folder. Once done, let me close Excel.
Ok, now we have our add-in file. If you want toĀ
share your add-in, you'll need to send your users
this file. To demonstrate the installation of theĀ
add-in, let me create a fresh new spreadsheet.
In this workbook, navigate to File, Options,Ā
Add-ins, and click on Go. In the new window,
click on Browse. This will open up the defaultĀ
add-in location folder. I recommend pasting your
add-in into this folder. So, I'll copy it fromĀ
our project folder and paste it here. Once done,
select it and hit OK. Make sure yourĀ
add-in is ticked before hitting OK again.
Alright, and that's it. We now have a newĀ
add-in. So, whenever I open a spreadsheet,
this add-in will be visible. To testĀ
it, I'll ask ChatGPT for a joke. Ok,
guys, and that's all I have for you. I hopeĀ
you found this video helpful. Good luck with
your own add-in. Thanks for watching,Ā
and I will see you in the next video!
Video Length: 13:22
Uploaded By: Sven Bosau
Published: 4/9/2023
View Count: 91,820