How To Use VBA Class Modules in Excel office 365?
Tags: tousevbaclassmodules, vbamacros
Published: 2021-09-13 11:44:39

---
title: How To Use VBA Class Modules in Excel office 365?
published: true
date: 2021-09-08 07:24:40 UTC
tags: ToUseVBAClassModules,VBAMacros
canonical_url: https://geekexcel.com/how-to-use-vba-class-modules-in-excel-office-365/
---

In this article, you will learn  **how to use VBA Class Modules in Excel Office 365**. Let’s see them below!! Get the official version of  **MS Excel**  from the following link: [https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)

## What is a Class in VBA?

A **class is a blueprint for an object** to be created. A class itself does nothing, but using classes you can create multiple identical objects that can perform operations or can be used as a data resource.

## Use VBA Class Modules

### Create your own class

- Firstly, in the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to **right-click on project explorer**.
- You need to click on **insert menu** and select **class module** in VBA and click on it.

![](https://geekexcel.com/wp-content/uploads/2021/09/Click-on-Class-Module.png)<figcaption>Class module</figcaption>

- After that, the **class** will be added to the **folder Class module**.
- Then, you can change the **class name** from the property window and name our class as **Welcome**.

![](https://geekexcel.com/wp-content/uploads/2021/09/Name-as-ClassModule.png)<figcaption>ClassModule</figcaption>

- Now, you have to create **add some attributes to our class**. Since, you want these attributes to be **available to the public** , and you have to use **accessibility operator public**.

```
Public name As String
Public var1 As Integer
Public var2 As Integer

```

- After that, you need to **add a function to this class**.
- For that, you have to **add a sub and name** it **sayHiTo**.

```
Sub sayHiTo(user As String)
    name = user
    MsgBox ("Hi! " & name)
End Sub

```

- Then, you need to use **class in a module** and insert a **new module** if you don’t have any.
- Now, you have to **write a sub** and named as **my sub Test**.

```
Sub test()
 Dim wc As New Welcome 'Declared and initialized Welcome object
 wc.sayHiTo ("Jack") 'used sayHiTo method of Welcome Object.
End Sub

```

- After that, you need to **run this sub Test** using **F5 key**.
- Finally, it will prompt, **“Hi! Jack”** on Excel workbook.

## Object Creation in Excel VBA

### Instant creation

- You have to **create an object** while declaring the **object with the “new” key**.
- In our example above, you need to use **instant creation**.

```
 Dim wc As New Welcome 
```

### Delayed creation

- You need to first **declare the object** only.
- Then, you don’t use the **new keyword**.
- Now, in order to **use the object** , you have to **initialize** it with the **“new” keyword**.

```
Sub test()
 Dim wc As Welcome
 'wc.sayHiTo ("Jack") 'generates error since wc is not initialised yet
'initialising object
Set wc = New Welcome
wc.sayHiTo ("Cory") 'this will work.
End Sub

```

## Accessing Variables of a Class

- You have used **public variables for class** , but it is wrong to practice.
- Then, you need to avoid using **public variables in a class**.
- Now, the question is how would we **access variables of the class**.
- After that, you have to use a **subroutine to access name** , but VBA classes provide properties that are used to systematically update and retrieve private variable values of the class. The properties are more elegant than sub or function for updating and accessing private variables.

**Syntax of class property**

```
 Private name As String
Private var1 As Integer
Private var2 As Integer
Property Let MyName(nm As String)
name = nm
End Property

Property Get MyName() As String
MyName = name
End Property
﻿
```

**Example**

- You have to  **copy and paste the code**  given below.

```
Sub test()
 'creating class object
 Dim wc As New Welcome
 Dim wc1 As New Welcome
 
 'using properties
 wc.MyName = "Exceltip.com"
 wc1.MyName = "ExcelForum.com"
 
 Debug.Print wc.MyName
 Debug.Print wc1.MyName
 
 End Sub
﻿
```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.
- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.
- Finally, you will get **two names** printed for **two objects of the Welcome class**.

## End of Era

We hope that this tutorial gives you guidelines on  **how to use VBA Class Modules in Excel Office 365. ** Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Read Next:**

- **[How to use the XNPV function in Excel office 365?](https://geekexcel.com/how-to-use-the-xnpv-function-in-excel-office-365/)**
- **[Excel Formulas to Highlight the Cells that End with Given Value!!](https://geekexcel.com/excel-formulas-to-highlight-cells-end-with/)**
- **[Excel Shortcut for “Select to End of Cell” in the Worksheet!!](https://geekexcel.com/quick-excel-shortcut-for-select-to-end-of-cell-in-a-worksheet/)**
- **[Excel Shortcuts: Delete to the End of Line in the Spreadsheet!!](https://geekexcel.com/shortcuts-for-delete-to-the-end-of-line-in-ms-excel-365/)**
- **[Excel Formulas for Counting Cells that End with certain character or text!!](https://geekexcel.com/excel-formulas-for-counting-cells-that-end-with-certain-character-or-text/)**