VBA String Array - Hoe String Array in Excel VBA declareren en initialiseren?

Inhoudsopgave

Excel VBA String Array

In VBA is een String-array niets anders dan een array-variabele die meer dan één stringwaarde met een enkele variabele kan bevatten.

Kijk bijvoorbeeld naar de onderstaande VBA-code.

Code:

Sub String_Array_Example () Dim CityList (1 tot 5) As Variant CityList (1) = "Bangalore" CityList (2) = "Mumbai" CityList (3) = "Kolkata" CityList (4) = "Hyderabad" CityList (5) = "Orissa" MsgBox CityList (1) & "," & CityList (2) & "," & CityList (3) & "," & CityList (4) & "," & CityList (5) End Sub

In de bovenstaande code heb ik als arrayvariabele gedeclareerd en de lengte van een array toegewezen van 1 tot 5.

Dim CityList (1 tot 5) als variant

Voor deze arrayvariabele heb ik 5 stadsnamen toegewezen met vermelding van elk array-aantal tussen haakjes.

CityList (1) = "Bangalore" CityList (2) = "Mumbai" CityList (3) = "Kolkata" CityList (4) = "Hyderabad" CityList (5) = "Orissa"

Vervolgens heb ik een code geschreven om deze plaatsnamen in het berichtvenster weer te geven.

MsgBox CityList (1) & "," & CityList (2) & "," & CityList (3) & "," & CityList (4) & "," & CityList (5)

Als ik deze code uitvoer, krijgen we een berichtvenster met alle plaatsnamen in één berichtvenster.

We weten allemaal dat dit zoveel tijd heeft bespaard in ons schema door de taak om individuele variabelen voor elke stad te declareren, te elimineren. Nog een ding dat u moet leren, is dat we nog steeds de regelcode die we schrijven voor stringwaarden kunnen verminderen. Laten we eens kijken hoe we code schrijven voor VBA-stringarrays.

Voorbeelden van String Array in Excel VBA

Hieronder staan ​​de voorbeelden van een Excel VBA-stringarray.

Voorbeeld 1

Zoals we in de bovenstaande code hebben gezien, hebben we geleerd dat we meer dan één waarde in de variabele kunnen opslaan op basis van de bepaalde matrixgrootte.

Wat we nu moeten doen, is de array-lengte niet ruim van tevoren bepalen.

Code:

Sub String_Array_Example1 () Dim CityList () As Variant End Sub

Zoals je hierboven tussen haakjes kunt zien, heb ik geen lengtes geschreven. Laten we nu voor deze variabele waarden invoegen met behulp van de VBA ARRAY-functie.

Geef binnen de array de waarden door met dubbele aanhalingstekens, elk gescheiden door een komma (,).

Code:

Sub String_Array_Example () Dim CityList () As Variant CityList = Array ("Bangalore", "Mumbai", "Kolkata", "Hyderabad", "Orissa") End Sub

Bewaar nu de oude code om het resultaat van plaatsnamen in het berichtvenster in VBA weer te geven.

Code:

Sub String_Array_Example1 () Dim CityList () As Variant CityList = Array ("Bangalore", "Mumbai", "Kolkata", "Hyderabad", "Orissa") MsgBox CityList (0) & "," & CityList (1) & " , "& CityList (2) &", "& CityList (3) &", "& CityList (4) End Sub

Een wijziging die ik in de bovenstaande code heb aangebracht, is dat we de ondergrens en de bovengrens van een arrayvariabele nog niet hebben bepaald, en we hebben de ARRAY-functie gebruikt, de array-telling begint bij 0, niet bij 1.

Dat is dus de reden dat we de waarden CityList (0), ClityList (1), CityList (2), CityList (3) en CityList (4) hebben genoemd.

Voer nu de code uit via Excel-sneltoets F5 of handmatig. We krijgen hetzelfde resultaat als we krijgen van de vorige code.

Voorbeeld 2

VBA String Array with LBOUND & UBOUND Functions

In case if you don’t want to show all the city lists in a single message box, then you need to include loops, define one more variable for loops.

Now to include FOR NEXT loop, we are not sure how many times we need to run the code. In this case, we can decide it like 5 times, but that is not the right way to approach the problem. So how about the idea of auto lower and higher level array length identifier???

When we open FOR NEXT loop, we usually decide the loop length as 1 to 5 or 1 to 10 depending upon the situation. Instead of entering the numbers manually, let’s use LBOUND and UBOUND function to decide on the lower value and upper value automatically.

For LBound and Ubound, I have supplied an array name, i.e., CityList. VBA LBound identifies the lower value of the array variable, and VBA UBound function identifies the upper value of the array variable.

Now show the value in the message box, instead of inserting the serial number, let the loop variable “k” takes the array value automatically.

Code:

Sub String_Array_Example1() Dim CityList() As Variant Dim k As Integer CityList = Array("Bangalore", "Mumbai", "Kolkata", "Hyderabad", "Orissa") For k = LBound(CityList) To UBound(CityList) MsgBox CityList(k) Next k End Sub

Now the message box will show each city name separately.

Example #3

VBA String Array with Split Function

Now assume you have city names like the below.

Bangalore;Mumbai;Kolkata;Hydrabad;Orissa

In this case, all the cities are combined together with the colon separating each city. In such cases, we need to use the SPLIT function to separate each city.

For Expression, supply the city list.

Code:

Sub String_Array_Example2() Dim CityList() As String Dim k As Integer CityList = Split("Bangalore;Mumbai;Kolkata;Hydrabad;Orissa", For k = LBound(CityList) To UBound(CityList) MsgBox CityList(k) Next k End Sub

The next argument is “Delimiter,” i.e., what is the one character that is separating each city from other cities. In this case, “Colon.”

Code:

Sub String_Array_Example2() Dim CityList() As String Dim k As Integer CityList = Split("Bangalore;Mumbai;Kolkata;Hydrabad;Orissa", ";") For k = LBound(CityList) To UBound(CityList) MsgBox CityList(k) Next k End Sub

SPLIT-functie split-waarden bepalen nu ook de hoogste array-lengte.

Dingen om te onthouden

  • LBOUND en UBOUND zijn functies om de array-lengtes te bepalen.
  • ARRAY-functie kan veel waarden bevatten voor een gedeclareerde variabele.
  • Als u eenmaal de ARRAY-functie wilt gebruiken, moet u niet beslissen over de array-lengte.

Interessante artikelen...