VBA Enum - Przykłady krok po kroku korzystania z wyliczeń VBA

ENUM to krótka forma wyliczeń, prawie w każdym języku programowania mamy wyliczenia, niektóre są wstępnie zdefiniowane, a niektóre są wyliczeniami zdefiniowanymi przez użytkownika, tak jak w VBA Vbnewline jest wyliczeniem i możemy wykonać własne wyliczenia za pomocą instrukcji ENUM.

Wyliczenia VBA (wyliczenie)

Zwykle deklarujemy zmienne i przypisujemy im typy danych. Typowe typy danych, których używamy, to „Integer, Long, Single, Double, Variant i String”. Ale mamy jeszcze jeden typ danych, np. VBA „Enum”. Pewnie myślisz, co to jest i wygląda dziwnie rzecz, ale aby rozwiać wszelkie wątpliwości, przedstawiamy ten artykuł na temat „Wyliczenia VBA”.

Co to jest VBA Enum?

„Enum” oznacza wyliczenia. Wyliczenie jest typem zmiennej, podobnie jak nasz ciąg znaków, liczba całkowita lub dowolny inny typ danych, ale tutaj tworzymy element listy za pomocą instrukcji Excel VBA Enum. Wyliczenie oznacza „czynność polegającą na wymienianiu kilku rzeczy jedna po drugiej”.

W programie Excel wyliczenie VBA jest typem zawierającym wyliczenie stałych. Wyliczenia tworzą listę elementów i tworzą je w grupie. Na przykład rodzaj telefonów komórkowych: „Redmi, Samsung, Apple, Vivo, Oppo”.

Używając wyliczeń, możemy zgrupować je wszystkie pod jedną wartością. Enum może być używane jako zmienne w VBA i jest to typ danych zmiennej numerycznej LONG.

Formuła VBA Enum

Jeśli niczego nie rozumiesz, nie martw się. Powoli to zrozumiesz. Teraz spójrz na formułę VBA Enum.

Enum GroupName Member1 = (Long) Member2 = (Long) Member3 = (Long) Member4 = (Long) Member5 = (Long) End Enum

Jak powiedziałem na początku, Enum może być używane jako zmienna i jest to typ danych zmiennej numerycznej Long.

Przykłady VBA Enum

Zanim przejdę do przykładów Enum, pokażę przykład „Constant” w języku VBA. Stała jest również słowem używanym do deklarowania zmiennej w VBA.

Spójrz na poniższe kody.

Kod:

Opcja Explicit Const Samsung = 15000 Const VIVO = 18000 Const Redmi = 8500 Const Oppo = 18500 Sub Enum_Example1 () End Sub

Zadeklarowałem zmienne w górnej części modułu za pomocą słowa Const.

Const Samsung = 15000

Const VIVO = 18000

Const Redmi = 8500

Const Oppo = 18500

Teraz wiem, że wszystkie te zmienne należą do grupy Mobile. Jeśli chcę użyć tych zmiennych, powiedzmy w module „Vivo”.

Kod:

Sub Enum_Example1 () V End Sub

Kiedy zaczynam znak „v”, widzę, że wiele innych rzeczy związanych z VBA jest pomieszanych z tymi, które zaczynają się na literę „v”.

W tym miejscu pojawia się obraz VBA „Enumerations”.

Aby lepiej zrozumieć, spróbujmy zmienić kolor tła komórki.

Kod:

Sub Enum_Example1 () AcriveCell.Interior.Color = RGB End Sub

Jak widać na powyższym kodzie, widzimy wszystkie kolory RGB dostępne w VBA. To są wszystkie stałe o cudownych nazwach.

Actually, all these RGB colors are part of the family enumeration called “xlRGBColor.”

Code:

Sub Enum_Example1() AcriveCell.Interior.Color = xlrg End Sub

By using these VBA enumerations actually, I can access to all the group member of this enumeration.

Code:

Sub Enum_Example1() AcriveCell.Interior.Color = XlRgbColor. End Sub

As we can see in the above image, we see only color combinations, nothing else. This is what the simple overview of the “VBA Enum.”

Ok, now we will go back to our original example of Mobile group members. Like how we have seen group members of RGB color similarly, we can declare the variables by using the VBA Enum statement.

Code:

Enum Mobiles Samsung = 15000 VIVO = 18000 Redmi = 8500 Oppo = 18500 End Enum Sub Enum_Example1() End Sub

Now I have declared all the mobile brands under the “Mobiles” group by using “Enum” statements.

By using the group name “Mobiles,” now I can access all these brands in the module.

Code:

Enum Mobiles Samsung = 15000 VIVO = 18000 Redmi = 8500 Oppo = 18500 End Enum Sub Enum_Example1() Mob End Sub

Select the group and put a dot to see all the members of the group.

Look, we can see only the group members of the group “Mobiles,” nothing else. This is how we can use VBA Enumerations to group a list of items under one roof.

Using VBA Enumeration Variables to Store the Data

Let’s see a simple example of using declared Enum variables. Declare Enum group name as “Department” and add department’s names as the group member.

Code:

Enum Mobiles Finance = 150000 HR = 218000 Sales = 458500 Marketing = 718500 End Enum Sub Enum_Example1() End Sub

I have declared each department’s salary numbers in front of them.

Now we will store the values of these numbers to excel sheet. Before applying the code, create a table like below.

Now go back to the basic visual editor and refer the cell B2 by using the RANGE object.

Code:

Sub Enum_Example1() Range("B2").Value = End Sub

In A2 cell, we have the Finance department, and so in B2 cell, we will store the salary of this department. So first, access the group name “Department.”

Code:

Sub Enum_Example1() Range("B2").Value = Dep End Sub

Now in this group, we can see only declared department names.

Code:

Sub Enum_Example1() Range("B2").Value = Department. End Sub

Select the department named “Finance.”

Code:

Sub Enum_Example1 () Range ("B2"). Value = Department.Finance End Sub

Podobnie dla wszystkich pozostałych komórek wybierz odpowiednie nazwy działów.

Kod:

Sub Enum_Example1 () Range („B2”). Value = Department.Finance Range („B3”). Value = Department.HR Range („B4”). Value = Department.Marketing Range („B5”). Value = Department .Sprzedaż End Sub

Uruchom ten kod VBA, a otrzymamy przypisaną kwotę wynagrodzenia dla tych działów.

W ten sposób możemy używać VBA Enum.

Możesz pobrać ten VBA Enum Excel tutaj. Szablon VBA Enum Excel

Interesujące artykuły...