VBA String Array - Hur deklarerar och initialiserar strängmatris i Excel VBA?

Innehållsförteckning

Excel VBA-strängmatris

I VBA är en strängmatris ingenting annat än en matrisvariabel som kan innehålla mer än ett strängvärde med en enda variabel.

Titta till exempel på nedanstående VBA-kod.

Koda:

Sub String_Array_Example () Dim CityList (1 To 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

I ovanstående kod har jag deklarerat som arrayvariabel och tilldelat längden på en array som 1 till 5.

Dim CityList (1 till 5) som variant

För denna arrayvariabel har jag tilldelat 5 stadsnamn som nämner varje arrayantal inom parentes.

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

Därefter har jag skrivit en kod för att visa dessa stadsnamn i meddelandefältet.

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

När jag kör den här koden får vi en meddelandefält som visar alla stadsnamnen i en enda meddelandefält.

Vi vet alla att detta har sparat så mycket tid från vårt schema genom att eliminera uppgiften att deklarera enskilda variabler för varje stad. Men en sak du behöver lära dig är att vi fortfarande kan minska linjekoden vi skriver för strängvärden. Låt oss titta på hur vi skriver kod för VBA-strängmatriser.

Exempel på strängmatris i Excel VBA

Nedan följer exemplen på en excel-VBA-strängmatris.

Exempel 1

Som vi har sett i koden ovan lärde vi oss att vi kunde lagra mer än ett värde i variabeln baserat på den matrisstorlek som bestämts.

Vad vi behöver göra är nu att inte bestämma matrislängden i god tid.

Koda:

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

Som du kan se ovan inom parentesen har jag inte skrivit några längder. Nu för denna variabel, låt oss infoga värden med hjälp av VBA ARRAY-funktionen.

Inne i matrisen skickas värdena på dubbla citat, var och en åtskilda av ett kommatecken (,).

Koda:

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

Behåll nu den gamla koden för att visa resultatet av stadsnamnen i meddelandefältet i VBA.

Koda:

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

En ändring som jag har gjort i ovanstående kod är eftersom vi inte har bestämt den nedre gränsen och övre gränsen för en matrisvariabel, och vi har använt ARRAY-funktionens matrisräkning börjar från 0, inte från 1.

Så det är anledningen till att vi har nämnt värdena som CityList (0), ClityList (1), CityList (2), CityList (3) och CityList (4).

Kör nu koden genom excel-genväg F5 eller manuellt. Vi får samma resultat som vi får från den föregående koden.

Exempel 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

Nu bestämmer SPLIT-funktionsdelningsvärden också den högsta matrislängden.

Saker att komma ihåg

  • LBOUND och UBOUND är funktioner för att bestämma matrislängderna.
  • ARRAY-funktionen kan innehålla många värden för en deklarerad variabel.
  • En gång om du vill använda ARRAY-funktionen, bestäm inte matrislängden.

Intressanta artiklar...