VBA स्ट्रिंग ऐरे - एक्सेल VBA में स्ट्रिंग ऐरे की घोषणा और शुरुआत कैसे करें?

एक्सेल VBA स्ट्रिंग ऐरे

VBA में, एक स्ट्रिंग सरणी एक चर चर के अलावा कुछ भी नहीं है जो एक एकल चर के साथ एक से अधिक स्ट्रिंग मान रख सकता है।

उदाहरण के लिए, नीचे दिए गए VBA कोड को देखें।

कोड:

उप String_Array_Example () मंद सिटीलिस्ट (1 से 5) के रूप में वेरिएंट सिटीलिस्ट (1) = "बैंगलोर" सिटीलिस्ट (2) = "मुंबई" सिटीलिस्ट (3) = "कोलकाता" सिटीलिस्ट (4) = "हैदराबाद" सिटीलिस्ट (5) = "उड़ीसा" MsgBox सिटीलिस्ट (1) और ", और सिटीलिस्ट (2) और", "और सिटीलिस्ट (3) और", "और सिटीलिस्ट (4) और", "और सिटीलिस्ट (5) एंड सब उप

उपरोक्त कोड में, मैंने सरणी चर घोषित किया है और सरणी की लंबाई 1 से 5 के रूप में निर्दिष्ट की है।

डिम सिटीलिस्ट (1 से 5) वेरिएंट के रूप में

इस चर चर के लिए, मैंने कोष्ठक में प्रत्येक सरणी गणना का उल्लेख करते हुए 5 शहर के नाम दिए हैं।

सिटीलिस्ट (1) = "बैंगलोर" सिटीलिस्ट (2) = "मुंबई" सिटीलिस्ट (3) = "कोलकाता" सिटीलिस्ट (4) = "हैदराबाद" सिटीलिस्ट (5) = "उड़ीसा"

इसके बाद, मैंने संदेश बॉक्स में इन शहर के नामों को दिखाने के लिए एक कोड लिखा है।

MsgBox CityList (1) और "," और सिटीलिस्ट (2) और "," और सिटीलिस्ट (3) और "," और सिटीलिस्ट (4) और "," और सिटीलिस्ट (5)

जब मैं इस कोड को चलाता हूं, तो हमें एक संदेश बॉक्स मिलेगा, जो एक संदेश बॉक्स में सभी शहर के नाम दिखाता है।

हम सभी जानते हैं कि इसने प्रत्येक शहर के लिए अलग-अलग चर घोषित करने के कार्य को समाप्त करके हमारे कार्यक्रम से इतना समय बचा लिया है। हालाँकि, एक और चीज़ जो आपको सीखने की ज़रूरत है वह यह है कि हम अभी भी स्ट्रिंग मानों के लिए लिखी गई लाइन के कोड को कम कर सकते हैं। आइए देखें कि हम VBA स्ट्रिंग सरणियों के लिए कोड कैसे लिखते हैं।

एक्सेल VBA में स्ट्रिंग ऐरे के उदाहरण

नीचे एक एक्सेल VBA स्ट्रिंग सरणी के उदाहरण दिए गए हैं।

उदाहरण 1

जैसा कि हमने उपरोक्त कोड में देखा है, हमने सीखा कि हम निर्धारित सरणी आकार के आधार पर चर में एक से अधिक मूल्य जमा कर सकते हैं।

अब हमें जो करने की जरूरत है वह पहले से ही सरणी की लंबाई को अच्छी तरह से तय न करें।

कोड:

सब स्ट्रिंग_अरे_प्रदर्शन 1 () मंद सिटीलिस्ट () वेरिएंट एंड सब के रूप में

जैसा कि आप कोष्ठक के अंदर देख सकते हैं, मैंने कोई लंबाई नहीं लिखी है। अब इस चर के लिए, चलो VBA ARRAY फ़ंक्शन का उपयोग करके मान डालें।

ऐरे के अंदर डबल कोट्स पर मानों को पास करते हैं, प्रत्येक को अल्पविराम (,) द्वारा अलग किया जाता है।

कोड:

सब स्ट्रिंग_अरे_प्रदर्शन () डिंपल सिटीलिस्ट () वेरिएंट सिटीलिस्ट के रूप में = ऐरे ("बैंगलोर", "मुंबई", "कोलकाता", "हैदराबाद", "उड़ीसा") एंड सब

अब VBA में संदेश बॉक्स में शहर के नामों का परिणाम दिखाने के लिए पुराने कोड को बनाए रखें।

कोड:

उप String_Array_Example1 () Dim CityList () वेरिएंट सिटीलिस्ट = Array ("बैंगलोर", "मुंबई", "कोलकाता", "हैदराबाद", "उड़ीसा") MsgBox CityList (०) & "," & CityList (१) और " , "और सिटीलिस्ट (2) और", "और सिटीलिस्ट (3) और", "और सिटीलिस्ट (4) सब

उपरोक्त कोड में मैंने एक बदलाव किया है क्योंकि हमने किसी ऐरे वेरिएबल की निचली सीमा और ऊपरी सीमा तय नहीं की है, और हमने ARRAY फ़ंक्शन का उपयोग किया है, जो काउंट अरेंज काउंट 0 से शुरू होगा, न कि 1 से।

तो, यही कारण है कि हमने सिटीलिस्ट (0), क्लिटलिस्ट (1), सिटीलिस्ट (2), सिटीलिस्ट (3) और सिटीलिस्ट (4) के रूप में मूल्यों का उल्लेख किया है

अब एक्सेल शॉर्टकट कुंजी F5 या मैन्युअल के माध्यम से कोड चलाएं। हमें वही परिणाम मिलता है जो हमें पिछले कोड से मिलता है।

उदाहरण # 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 फ़ंक्शन स्प्लिट वैल्यू भी उच्चतम सरणी लंबाई निर्धारित करते हैं।

याद रखने वाली चीज़ें

  • सरणी लंबाई निर्धारित करने के लिए LBOUND और UBOUND फ़ंक्शन हैं।
  • ARRAY फ़ंक्शन किसी घोषित चर के लिए कई मान रख सकता है।
  • एक बार यदि आप ARRAY फ़ंक्शन का उपयोग करना चाहते हैं, तो सरणी लंबाई तय न करें।

दिलचस्प लेख...