VBA Collection Hur skapar man samlingsobjekt i VBA?

Innehållsförteckning

Excel VBA-samlingsobjekt

I VBA-kodning kan vi, förutom den befintliga samlingen av objekt under en grupp, skapa våra samlingsgrupper. I våra många artiklar har vi talat om objektvariabler, och i denna handledning kommer vi att gå igenom VBA-samlingsobjektet i detalj.

Om du har gått igenom vår tidigare artikel "VBA Arrays" blir det mycket lättare för dig att förstå. Arrays används för att gruppera variabler under ett tak; På samma sätt används också samling för att lagra en grupp variabler.

Samlingar används för att lagra objekt. De är mycket mer flexibla än VBA-matriserna, medan matriser har fasta storleksgränser, men läsare har ingen fast storlekstorlek vid någon given tidpunkt och behöver inte ens ändra manuell storlek.

VBA-samlingen liknar mycket ”VBA-ordlistan”, men ordboken kräver att extern objektreferens ställs in under objektreferensfönstret. Med VBA Dictionary måste vi ställa in referens typen som "Microsoft Scripting Runtime", men samlingen kräver inga extra tillbehör.

Hur skapar man samlingsobjekt i VBA?

För att komma igång med insamlingen först måste vi deklarera variabeln som "Samling."

Koda:

Sub Collection_Example () Dim Col Som Collection End Sub

Eftersom samlingen är en objektvariabel måste vi ställa in objektreferensen genom att skapa en ny instans.

Koda:

Sub Collection_Example () Dim Col Som Collection Set Col = New Collection End Sub

Nu med variabeln kan vi komma åt alla metoder för samlingsvariabeln "Kol."

Koda:

Sub Collection_Example () Dim Col As Collection Set Col = New Collection Col. End Sub

Innan vi använder dessa metoder måste vi deklarera en variabel som en sträng.

Koda:

Sub Collection_Example () Dim Col As Collection Set Col = New Collection Dim ColResult Som strängänd Sub

Använd nu variabeln "Col" för att välja "Add" -metoden.

Koda:

Sub Collection_Example () Dim Col Som Collection Set Col = New Collection Col.Add End Sub

Under metoden Lägg till har vi specifika parametrar. Låt oss anta att vi lagrar mobila varumärken med deras genomsnittliga försäljningspris på marknaden.

Under artikel anger argumentet mobilens pris.

Koda:

Sub Collection_Example () Dim Col Som Collection Set Col = New Collection Col.Add Item: = 15000, End Sub

Nästa under Key argument anger mobila varumärke.

Koda:

Sub Collection_Example () Dim Col Som Collection Set Col = New Collection Col.Add Item: = 15000, Key: = "Redmi" End Sub

För variabeln "ColResult" lagrar vi resultatet av "Col" -objektvariabeln.

Koda:

Sub Collection_Example () Dim Col Som Collection Set Col = New Collection Col.Add Item: = 15000, Key: = "Redmi" ColResult = Col (End Sub

När du öppnar parentesen för variabeln "Col" kan vi se argumentet som Index. För detta argument måste vi ange det kritiska argumentvärdet från metoden Collection-tillägg, dvs. namnet på det mobila varumärket.

Koda:

Sub Collection_Example () Dim Col Som Collection Set Col = New Collection Col.Add Item: = 15000, Key: = "Redmi" ColResult = Col ("Redmi") End Sub

Låt nu resultatet visas i meddelandefältet i VBA.

Koda:

Sub Collection_Example () Dim Col Som Collection Set Col = New Collection Col.Add Item: = 15000, Key: = "Redmi" ColResult = Col ("Redmi") MsgBox ColResult End Sub

Okej, vi är klara när vi kör koden. Vi borde se priset på mobilmärket ”Redmi”.

Bättre förståelse för nyckel- och artikelparametrar

Jag är säker på att det inte är lätt att förstå parametrarna för samlingsobjektet. Låt mig förklara för dig ett enkelt exempel.

Tänk dig att du har en fruktmeny med deras namn och pris på frukten. Antag att du söker fruktpriset "Apple" med namnet på frukten.

To search the price of the fruit, we need to mention the name of the fruit, i.e., in collection VBA language Name of the Fruit is Key, and the price of the fruit is “Item.”

It is like applying the VLOOKUP or HLOOKUP function, based on the lookup value, and we will fetch the required data from the database. Here lookup value is Key, and the result is Item.

Advanced Example

Imagine you are a store manager in one of the retail stores, and you are responsible for managing the customer queries. One such customer query is an inquiry about the product price.

It would help if you allowed the customer to search the price of the product with complete information. Similarly, you need to show the message in case of no data found. Below is the example code which will present the input box in front of the user. They require to enter the name of the product they are looking for. If the product is there in the collection, it will show the price of the mentioned product, or else it will show the message as “The Product you are searching for doesn’t exist.”

Code:

Sub Collection_Example2 () Dim ItemsCol Som Collection Dim ColResult Som stränguppsättning ItemsCol = New Collection ItemsCol.Add Key: = "Apple", Item: = 150 ItemsCol.Add Key: = "Orange", Item: = 75 ItemsCol.Add Key: = "Water Melon", Item: = 45 ItemsCol.Add Key: = "Mush Millan", Item: = 85 ItemsCol.Add Key: = "Mango", Item: = 65 ColResult = Application.InputBox (Prompt: = "Please Ange fruktnamnet ") Om ItemsCol (ColResult)" "Då MsgBox" Priset på frukten "& ColResult &" är: "& ItemsCol (ColResult) Annars MsgBox" Priset på den frukt du letar efter finns inte i samlingen "End If End Sub

Intressanta artiklar...