How to use Enumeration in Excel VBA Easily (2 Example)

Welcome to Excel Avon

Enumeration in Excel VBA

Enum is compressed as enumeration. It is a process in which all are mentioned separately and refer to something. Similarly, we have calculations in every programming language, be it Java, C or Excel VBA. Enum is actually a variable just like integer or string and string. we can define a group value by a variable name, it must be numeric long type value for each member of group. We can make our enumerations using the Enum statement. Let’s go through VBE and understand how Enum is used.

First, we will go to Developer tab and then go to Visual Basic.

enum-in-excel

First go to the INSERT tab and then I will insert module.

enumeration-in-excel-vba

How to use Enumeration in Excel VBA

DOWNLOAD USED EXCEL FILE FROM HERE>>

When we enter in VBE, I will declare the Enumeration function with an Enum name.

Enum xlcolor

End Enum

Then I will declare all Enum Values, here I will write value of white, black, blue and green.

Enum xlcolor
xlwhite = 16777215
xlBlack = 0
xlBlue = 6299648
xlgreen = 5287936
End Enum

Add comments to subroutines. Now I will use this enumeration in the subroutine, will write the subroutine.

Enum xlcolor
xlwhite = 16777215
xlBlack = 0
xlBlue = 6299648
xlgreen = 5287936
End Enum

Sub UseEnum()
dim Color as xlcolor

After writing the subroutine, define the color variable.

Enum xlcolor
xlwhite = 16777215
xlBlack = 0
xlBlue = 6299648
xlgreen = 5287936
End Enum

Sub UseEnum()
dim Color as xlcolor

'Define color Variable
Color = xlblue
End Sub

I will write range for filling color for which the variable is written. Use interior color function 

Enum xlcolor
xlwhite = 16777215
xlBlack = 0
xlBlue = 6299648
xlgreen = 5287936
End Enum

Sub UseEnum()
dim Color as xlcolor

'Define color Variable
Color = xlblue

Activesheet.range("B12:B15).Interior.color = color
End Sub

After writing the code we will click run button

Enum-inexcel

After clicking the run button, we will go to the active sheet of excel, thus the color of the range will be added.

enumeration in excel avon

To change the black color range or range color, we need to go to VBE and then change the define color variable.

Enum xlcolor 
xlwhite = 16777215 
xlBlack = 0 
xlBlue = 6299648 
xlgreen = 5287936 
End Enum 

Sub UseEnum() 
dim Color as xlcolor 

'Define color Variable 
Color = xlBlack
 
Activesheet.range("B12:B15).Interior.color = color
End Sub

After writing the code we will click run button

Enum-inexcel

After clicking the run button, we will go to the active sheet of excel, thus the color of the range will be added.

enumeration in excel

        • To use enumeration in VBE we demonstrate by name Enum.
So, I hope you have understood How to use Enumeration in Excel VBA and for more information, you can follow us on Twitter, Instagram, LinkedIn, and YouTube as well.
You can also see well-explained video here about Logical Operator

Leave a Reply