Create Class and Form in Microsoft Excel using VBA


Like any other language we can create Class and windows form using VBA in Microsoft Office 2013 Excel. VBA Classes and Forms also will behave as normal windows form and class.

Open Microsoft Office Excel 2013 => Add Command Button, add Class Module to the Excel file by right click on VBA project as shown below.

                        

 

Now add Id, Name, and Company Name properties to the Class Module as shown below.

Public Id As String

Public Name As String

Public CompanyName As String

Now Add UserForm as shown below.

                         

Add Labels and TextBoxes for the Id, Name and CompanyName as shown below.

           

Now add the below code for Command Button click.

Private Sub CommandButton1_Click()

    Dim objEmp As New Employee

    objEmp.Id = "000100"

    objEmp.Name = "Raj"

    objEmp.CompanyName = "Microsoft"

   

    Dim objForm As New UserForm1

    objForm.txtId = objEmp.Id

    objForm.txtName = objEmp.Name

    objForm.txtCName = objEmp.CompanyName

   

    objForm.Show

End Sub

As shown above we created the object for Employee class and assigning these values to User Form. Now if we click on the button the output display as shown below.