In this video, we're going to talk about what objects are in VBA and the corresponding object hierarchy
that we should always pay attention to while we're coding in VBA.
We often reference things called objects, objects, or essentially the different things you might interact
with from your code like worksheets or tables.
And they live in an object hierarchy that we have to pay attention to.
The object hierarchy starts with Excel itself, which is the application object.
The application object in this case, Excel contains other objects that we can interact with.
For example, workbooks would be the next object in our hierarchy.
And workbooks are your actual Excel files.
So if you have several Excel files open at once.
We can use VBA to interact with all of them, even if they don't have embedded macros because they're
objects that we can reference.
After workbooks comes worksheets, which are obviously the individual sheets that you would have in
your workbooks and sheets, have a lot of different object types that we can interact with, ranges,
sells tables, and all of those have different things that we can do.
So this object hierarchy continues downwards.
So you can see here I've written how we would reference each of these objects in VBA.
Application is pretty simple.
It's just application for workbooks.
We would enter the name of the workbook that we want to look at for worksheets.
You can either use the name of the worksheet or the number of the worksheet if your sheets are always
in the same order and so on and so forth, all the way down through the object hierarchy.
And there will be videos that dive into each of these object types a little deeper and how to use them
in VBA.
Now, the other thing to note is that there are different things we can do with each of these objects.
The way we interact with the Excel application object, for example, might be to tell a system to stop
Diplock displaying alert messages or stop trying to update the screen graphics.
But when we interact with the workbook object, our actions might be to save or open a new file.
So the important thing to note about the hierarchy is that we have to walk VBA through the hierarchy
in the correct order in order to get what we want out of it.
For example, if we wanted to retrieve data from a specific cell in our Excel file, let's say this
highlighted cell over here, we would need to tell VBA first.
We are in the Excel application.
Second, I want to look at this workbook called example.
Third, I want to look at the object hierarchy sheets specifically and last.
I am interested in cell two eight, which means row two.
Column eight.
In this case.
So each two.
And so you can see what this would look like if we spit it out into a full Excel statement.
Now, that period that goes after each object, it tells VBA that we are about to move another step
further into the hierarchy.
So we start with the application and the period tells VBA we're about to move into the second layer
of the hierarchy.
In this case, the workbooks.
And we reference this workbook that we're in now, which is example.
So this is what the full statement would look like in VBA.
And this is what we would type if we wanted to reference this particular cell we have highlighted in
yellow.
We can't move any of these objects to different places in the hierarchy.
They have to fall out from top to bottom like we have here.
So that means we can't start by telling Excel we want to see what's in cell two eight and then start
backpedaling to say and it's in the example workbook.
And I want to use Excel and I want to use the sheet in particular.
We have to follow this object hierarchy.
And this can get a bit tedious when we're doing a lot of heavy data manipulation.
So there are ways that we can skip some of these steps.
And if we cut out the earlier steps in the process, let's say we get rid of the application and the
workbook steps and we start with our worksheet and then specify the cell.
VBA will assume the application and workbook or whatever is active at that moment in time.
And what this means is that VBA will assume we're using Excel since we have Excel Open and we'll assume
that we're using the example workbook because that's what I have open right now that I'm clicked into.
If I open another Excel file and put my cursor in that second Excel file, then the example workbook
here would be inactive, but we would still be in the Excel application.
If I open something like an access database, then we wouldn't even be in the Excel application anymore.
So you can see here I have sheets down at the bottom and this is a pretty good example of what is active
and what is not.
I have three sheets down here.
Object hierarchy, workbook objects and worksheet objects.
So if I click on Object Hierarchy like I am now, we can see that the sheet is highlighted.
It's clear that this is the sheet that I'm on.
This is my active sheet.
If I click onto one of these sheets, you can see this becomes highlighted and this is my active sheet
and I can toggle back and forth between these sheets to make one active and the other two inactive.
So we can take shortcuts when we're working with our object hierarchies.
But it's important that we understand how those shortcuts are impacting our code.
And there are some tic tips and tricks that we can use to make it easier to reference our full object
hierarchy.
If we're worried about introducing risk or error into our code.
So we'll talk through all of that as we talk about these objects in VBA.
So in this video, we looked at an overview of the object.
And how it impacts our code.
So now we're ready to take a closer look at each of the object types.