Open In App

Workbook and Worksheet Object in Excel VBA

Last Updated: 30 Jan, 2023

A

Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Excel VBA is an object-oriented programming language, which means in excel every component is an object. The superset of all the objects of excel is the excel application itself. The Excel Application Object is the root of all the other objects in excel. It contains Workbook Object. The Workbook Object contains another object such as Worksheet Object which includes all the worksheets present in a particular workbook. The Worksheet Object contains Cells Object. To perform any task or operation in excel we need to follow this excel object hierarchy.

Excel-Object-Hierarchy

 

Workbooks Object

In excel VBA, the workbooks object represents the collection of the workbook which are open in the excel application at the same time. This is the reason we write it as plural- Workbooks Object, which means it contains more than one workbook. In order to access a single workbook from the workbooks object, we can use the following VBA to access it from its name.

command-to-access-workbook

 

How to Refer a Workbook in VBA?

To refer a workbook in excel VBA, we can use the following different methods:

By Name

We can easily refer to a workbook using its name. We need to define a procedure in VBA and declare our excel workbook name. Make sure that the user name is only if the workbook is not saved otherwise we need to define the name along with its extension.

Referring-to-a-workbook

 

By Number

The excel application maintains an index order for all the workbooks we open. It gives numbers 1 to the first workbook, 2 to the second workbook, and so on. We can also use these numbers to refer to the workbook.

Note: It became really difficult to remember which workbook we have opened in which order. So, it is quite less used.

referring-by-number

 

By ActiveWorkbook Property

We can refer to a workbook using the ActiveWorkbook property. Using ActiveWorkbook we can refer to the workbook which is in an active state, which means the workbook is open in excel.

using-activeworkbook

 

Access Methods and Properties of a Workbook

Like any other object-oriented programming language, In VBA whenever we refer to an object it gives us access to all the methods and properties of that object. Similarly, when we access the Workbook Object, we get access to all its properties and methods.

Accessing Methods of Workbooks Object

In order to access the methods of the workbook object we will use the dot(.) operator. Once, we use our workbooks object as soon as we will use the dot(.) operator, it will display the list of methods associated with that particular object.

Access-methods-of-Workbooks-object

 

Accessing Properties of Workbooks Object

Similar to methods, we can use the dot(.) operator to access the properties of the workbook object.

Accessing-properties

 

As we can see, we have 3 different sheets currently open in our excel, if we run our procedure it will display the count as 3 in a message box.

Output

 

Worksheets Object

In excel VBA, the worksheets object represents the collection of the worksheets which are open in the excel workbook. This is the reason we write it as plural- Worksheets Object, which means it contains more than one worksheet.

How to Refer a Worksheet in VBA?

By Name

In excel, we can refer to a sheet using its name. By default, Excel creates sheets in natural number ordering. For example, Sheet1, Sheet2, etc. We can also give our own name to these sheets.

referring-by-name

 

By Number

We can also use sheet numbers to refer to a sheet in excel. The two operations to refer to a sheet, Sheets() and Worksheets() work in different ways while accessing the sheets by number.

Operation

Working

Sheets(n)

This is used to refer to the nth sheet starting from the first sheet

Worksheets(n)

This is used to refer to the nth sheet itself.
referring-by-number

 

By ActiveSheet Property

In excel VBA, we can also refer to a sheet using the ActiveSheet property.

referring-by-activesheet-property

 

As above, we can see we have the GFGCourse sheet active. When we run our procedure, it will pop up a message box with the active sheet name is.

Output

 

Access Method and Properties of a Worksheet

Similar to the Workbook object, Worksheet Object also comes up with different methods and properties. In excel VBA, we can access methods and properties of excel worksheet objects using the dot(.) operator.

Methods-and-properties-of-worksheet

 



A

News
Improve
Discuss
Do you want to advertise with us?Click here to know more

How to Create Charts in Excel Using Worksheet Data and VBA?

article_img
Excel is an important software provided by Microsoft Corporation. This software belongs to one of the major software suites Office 365. In this software suite, there are other software are present like Word, PowerPoint, etc. They are called Office 365, as this software are mostly used for office purpose. But now the world has changed a lot. After the Corona Pandemic, the world knows the positivity of using digital tools. Office 365 was not different from that. As a part of the software suite, Excel software also gains some importance from the users. They are not only used for official purposes. But they can also be used for school purposes. Excel is software that can able to store data in an effective form. So, searching for the data becomes more manageable in this software. Excel has another great feature. It can be used to derive the charts from the provided data. The charts are helpful for analyzing any growth of the data. If there are thousands of data present, it is a difficult task to extract some analysis from that data. But if those data are converted to charts, then it will be easy to analyze those data. Excel sheet helps to do the same. Charts can be prepared whatever the
Read More

Excel Personal Macro Workbook

article_img
When we create the macros for automation, they can be used for that particular workbook only. We cannot access them outside that workbook in others. Excel Personal Macro Workbook is a hidden workbook in your local system that pops up whenever you open the Excel application. It is a place where all the macros from any workbook can be stored and one can use the macros from here which are often required. How to create a personal macro workbook? Creating a personal macro workbook is similar to creating a macro in Excel. Let's discuss it in some simple steps : Click on the developer tab on the ribbon and then click on the Record Macro tool in the Code group.A new dialog box would open.In the option, "Store Macro In", choose Personal Macro Workbook. In this way, our macro would be stored in the personal macro workbook.After filling the dialog box, click OK.In the last, click on stop recording in the developer tab. This is how a personal macro workbook can be created. How to view, create, copy and edit macros in Personal Macro Workbook? All the options like view, create, copy and edit macros can be done from VBA(Visual Basic for Applications) Editor. Let's discuss it in some simple ste
Read More

How to Remove Smart Tags from a Workbook in Excel

article_img
Excel's Smart Tags offer a surprising assortment of capabilities meant to expedite your workflow and better data management, which are typically neglected. These small, dynamic icons show up quickly in your spreadsheets and give you options relevant to the current situation to make typical jobs more accessible and reduce the amount of work you must do by hand. But in the name of speed, their constant presence can sometimes backfire, causing spreadsheets to get cluttered and processes to get broken up. This article will show you how to remove Excel smart tags from a worksheet. What are Smart Tags?When you do something with data in a worksheet, smart tags appear as little icons under the cells. The most common smart tag shows and offers many paste alternatives when you copy and paste data. When you paste data, this symbol appears beneath the paste range. When you click on it, various paste choices will show, such as paste values, paste formulae, transpose, and more. Excel automatically uses smart tags. Types of Tags in Excel WorkbookSmart Tags are no longer available in later versions of Excel because they were taken out of service in 2010. In the past, they were context-sensitive ch
Read More

How to Protect a Workbook in MS Excel?

article_img
Every day in school, offices, business sectors or any other field lots of information are there that are required to store for the future use. For anyone, it is very difficult to remember that information for a long time. Earlier data and information are stored in a form of a register, file, or paperwork but finding it may be difficult for us. It takes a lot of time. To resolve this issue Microsoft brings a software called MS Excel. Microsoft Excel is a software that allows users to store or analyze the data in a proper systematic manner. It uses spreadsheets to organize numbers and data with formulas and functions. MS Excel has a collection of columns and rows that form a table. Generally, alphabetical letters are assigned to columns, and numbers are usually assigned to rows. The point where a column and a row meet is called a cell. Generally, it is used to keep records, data in various fields of business. Some features of MS Excel: Auto calculate- This helps you to calculate a group of data, and it automatically shows the result in the status area You can also create your own formulas.Shared- You can share your data file over a network.Templates- It gives us a variety of elegantl
Read More

VBA Subroutine in Excel - How to Call Sub in VBA?

article_img
When a specified action is performed on a worksheet with the help of a collection of code known as a VBA Subroutine. It also helps to read an external file, also it can open other applications from Excel. A large piece of code can be broken into small parts so that we can manage it easily. Let's learn why to use submarines: Converts large piece of codes into small parts so that the computer ignores all kind of complexities that arises because of large codesReusability of code suppose we in a program have to access the database frequently so instead of writing the code again and again we can create a function to access the databaseSubroutines are self-documenting functions which means a coder can easily say what the program does by looking into the name of the function Naming Rules of SubroutinesIt can start with a letter or an underscore but it cannot start with a number or a special character.It cannot contain any space in the name.The name of the subroutine cannot be a keyword like Private, Sub, End, etc. Syntax Private Sub function_name( ByVal arg1 As String, ByVal arg2 As String) End Sub Syntax Explanation Code Action "Private Sub function_name(...)"Private is the keyword whic
Read More

Basic Object Model in Excel VBA

article_img
VBA stands for visual basic for application. Excel VBA is an object-based programming language, it is used while recording a macro i.e., it uses the idea of Encapsulation and stores the state (data) and operation (functions) inside the object. Excel objects are arranged in a hierarchy that governs the excel object. Every element in excel is represented by an object in VBA. Below are the basic excel elements, Excel Objects In the above picture, we can have the following different objects: Workbook Object - We have one excel workbook open so, we have 1 workbook object. For example, if we have 3 different excel workbooks, then we have 3 workbook objects.Worksheet Object - A workbook object has a worksheet object. Here, we have 3 different worksheet objects.i.e., Sheet1, Sheet2, and Sheet3.Range Object - Inside a worksheet object we can have a range object. Here, we have RangeObject(B5:D9).ActiveCell Object - Inside a range object or a worksheet object we can have a cell object.Cell Object - Inside a cell object we can have Font, Size, Color, Value, Border, and so on.., various objects. The excel objects are arranged in a hierarchy and they act as the container for the other excel obje
Read More

Variables and Data Types in VBA Excel

article_img
In a computer system, variables and data types are almost used in every program to store and represent data. Similarly, Excel VBA also has variables and data types to store and represent data and its type. In this article, we will learn about VBA variables, their scope, data types, and much more. VBA Variables VBA(Visual Basic for Application) variables are similar to other programming languages variables, they act as a container that is used to store data(integer, string, floats, etc). We can use the variables in the code at multiple places and executer the programs. Defining Variables In VBA VBA gives permission to define variables in two ways: Implicitly - In VBA, we can implicitly declare variables using the assignment(=) operator. All the variables that are implicitly declared in VBA are of type "Variant". The variant type variables required more memory space than usual variables. Example: label="gfg"Explicitly - Explicitly we can declare variables using "Dim" keyword. Explicit variable also reduces the naming conflicts and spelling mistakes. Example: Num as password Syntax For VBA Variables // macro definition Sub VBA_Variable_Example () Dim <name> End Sub VBA V
Read More

Function and Sub in Excel VBA

article_img
In Visual Basic, the functions and sub-procedures play similar roles but have different or unique characteristics. However, both perform a programmed task. They utilize a set or group of commands to deliver the required results. The key difference between the sub and the functions is that a sub-procedure generally does not return a result whereas functions tend to return a result. Hence if there is a need for having a value post execution of tasks, then place the VBA code under a function or otherwise place the code under a sub-procedure. In Excel, there is the availability of large numbers of VBA functions that could be utilized in the development of new VBA codes. Such functions are referred to as Built-in functions. With the increase in the size of a VBA program, both Functions and Sub-procedures play a crucial role in the management and performance of VBA code. Functions in VBA A function in VBA can be defined as a procedure that executes a piece of code or instructions and post-execution, it returns the value of the tasks performed. A function is hence invoked using a variable. Functions are directly called in the spreadsheets by using excel based formulas. An excel VBA functi
Read More

VBA Date and Time Functions in Excel

article_img
Date and Time Functions are the inbuilt functions that give us the opportunity to see the date or time according to the user's need. Suppose a user needs to see the month or the day or the year then it can be easily seen by different date functions. Similarly, for the time function, also we can manipulate it according to the need of the user. Date and Time functions are used to interconvert date and time in different formats. In this article, we will learn about the most commonly used date and time functions. VBA Date Functions There are fifteen-plus different date functions in VBA, but here we will talk about some of the most commonly used date functions. VBA Date Function The Date() function returns the current date. The Date() function does not require any arguments. For example, declare a variable name date_1 of Date data type, call the Date() function, and store the return value in date_1, then print the date_1 in the console. Syntax of the function: Date() VBA DateAdd Function The DateAdd() function is used to add an interval of date/time to the respective date or time. The function will return the resulting date or time. The function takes three arguments, Interval, Nu
Read More

How to Declare and Initialize String Array in Excel VBA?

article_img
A string array is an array where we can store only string values in the array, with the help of a string array, we can store more than one string value. We can declare the string array in many ways like declaring a static string array, declaring a variant size array of string using the Array function, and a string array using the split function which we will discuss in this article Declaring a Static String Array A static Array is an array whose size is fixed and it can be declared in two ways one is declared implicitly and another one is explicit. The following code is to declare a string array implicitly. The following code is to declare a string array explicitly. Declaring a Variant Size Array of String using Array Function In the following code an array is declared with variant size and string values are initialized using the array function: If we want to access the strings in the array then we have to write, Declaring a String Array using Split Function The following code is to declare an array without any fixed size and a split function is used to assign the string values. If we want to access the strings in the array then we have to write, By default, the lower bound
Read More
three90RightbarBannerImg