What is Array and Why We Use Array in VBA in Hindi
Makhanlal Chaturvedi University / BCA / VBA programming
Understanding Arrays in VBA with Examples in Hindi
What is Array and Why We Use Array in VBA in Hindi
जब हम VBA (Visual Basic for Applications) में programming करते हैं, तो कई बार हमें एक जैसे type के बहुत सारे data को एक साथ store करने की आवश्यकता होती है। मान लीजिए हमें 100 students के marks store करने हैं, तो क्या हम 100 अलग-अलग variables बनाएंगे? नहीं, यही समस्या हल करने के लिए हम Array का उपयोग करते हैं।
Array एक ऐसा variable होता है जिसमें एक ही नाम से कई values को store किया जा सकता है। हर value की पहचान उसके index या position से होती है। Array हमें memory को efficiently उपयोग करने में मदद करता है और programming को बहुत आसान बना देता है।
Why We Use Array in VBA
- एक जैसे type के multiple data को एक साथ store करने के लिए
- Code को short और readable बनाने के लिए
- Loops के साथ मिलाकर repetition को manage करने के लिए
- Memory को efficiently use करने के लिए
- Dynamic और complex data structures को handle करने के लिए
Declaring an Array with Dim Statement in VBA in Hindi
VBA में Array को declare करने के लिए हम Dim statement का प्रयोग करते हैं। Dim का full form होता है "Dimension" और यह किसी भी variable को define करने के लिए उपयोग किया जाता है। जब हम Array बनाते हैं, तो हमें यह भी तय करना होता है कि उसमें कितने elements होंगे।
Syntax of Declaring Array
Dim arrayName(index) As DataType
Example
Dim marks(4) As Integer
इसका मतलब है कि हमने एक Array बनाया है जिसका नाम marks है और उसमें 5 elements store हो सकते हैं (index 0 से 4 तक)। यानी marks(0), marks(1), ..., marks(4)।
Types of Arrays
- Fixed-size Array: जिसकी size पहले से तय होती है। जैसे
Dim names(5) As String - Dynamic Array: जिसकी size को runtime पर बदला जा सकता है। जैसे
Dim data() As Integer, फिरReDim data(10)
Understanding Indexing and Boundaries of Array in Hindi
VBA में Array की indexing 0 से शुरू होती है। यानी अगर आपने लिखा Dim arr(5), तो उसमें total 6 elements store किए जा सकते हैं (arr(0) से arr(5) तक)। यह बात बहुत महत्वपूर्ण है क्योंकि कई बार beginners यह सोचते हैं कि arr(5) का मतलब 5 items होंगे, लेकिन असल में 6 होते हैं।
Lower Bound और Upper Bound
- Lower Bound: Array की शुरुआत का index (default 0 होता है)
- Upper Bound: Array का आखिरी index, जो कि आपने define किया होता है
Index Check करने के लिए Function
LBound(arrayName) ' Lower Bound
UBound(arrayName) ' Upper Bound
Example
Dim marks(3) As Integer
MsgBox LBound(marks) ' Output: 0
MsgBox UBound(marks) ' Output: 3
इससे हमें यह पता चलता है कि Array की शुरुआत और अंत कहां है, जिससे हम Loop के अंदर आसानी से Array को access कर सकते हैं।
Examples of Using Array to Store Multiple Values in Hindi
अब हम कुछ practical examples देखते हैं कि Array का उपयोग करके कैसे multiple values को store और process किया जा सकता है। यह examples beginners के लिए बहुत ही आसान और सीखने योग्य हैं।
Example 1: Students के Marks को Store करना
Sub StoreMarks()
Dim marks(4) As Integer
Dim i As Integer
' Values assign करना
marks(0) = 45
marks(1) = 50
marks(2) = 65
marks(3) = 70
marks(4) = 80
' Display करना
For i = 0 To 4
MsgBox "Student " & i + 1 & " Marks: " & marks(i)
Next i
End Sub
Example 2: Names को Array में Store करना
Sub StoreNames()
Dim names(2) As String
names(0) = "Amit"
names(1) = "Ravi"
names(2) = "Suman"
MsgBox "Second student is: " & names(1)
End Sub
Example 3: Dynamic Array का Use
Sub DynamicArrayDemo()
Dim numbers() As Integer
Dim i As Integer
ReDim numbers(5)
For i = 0 To 5
numbers(i) = i * 10
Next i
For i = 0 To 5
MsgBox "Value at index " & i & " is: " & numbers(i)
Next i
End Sub
Example 4: Sum of Array Elements
Sub SumArray()
Dim scores(4) As Integer
Dim total As Integer
Dim i As Integer
scores(0) = 10
scores(1) = 20
scores(2) = 30
scores(3) = 40
scores(4) = 50
For i = 0 To 4
total = total + scores(i)
Next i
MsgBox "Total Score: " & total
End Sub
Table: Comparison Between Variable and Array
| Feature | Variable | Array |
|---|---|---|
| Data Storage | Single value | Multiple values |
| Efficiency | Less efficient | More efficient |
| Memory Usage | More variables needed | Single variable |
| Best For | One-time use | Loops and repetition |
इन सभी उदाहरणों से आपको यह समझ में आया होगा कि Array का उपयोग VBA में कितना उपयोगी और आसान है। जब भी आपको एक जैसे type के कई data को संभालना हो, Array सबसे अच्छा समाधान होता है। चाहे वो student marks हों, employee names हों, या किसी report के figures हों, हर जगह आप Array का smart तरीके से इस्तेमाल कर सकते हैं।
FAQs
Dim arrayName(size) As DataType के रूप में declare किया जाता है। उदाहरण: Dim marks(4) As Integer एक integer array declare करता है जिसमें 5 values store हो सकती हैं।
Dim arr(3) declare करते हैं तो उसमें arr(0) से arr(3) तक कुल 4 elements होंगे।
ReDim की मदद से बदला जा सकता है। यह flexibility देती है changing data को handle करने के लिए।
For loop का उपयोग करके Array के सभी elements को access कर सकते हैं। जैसे:
For i = 0 To UBound(marks)
MsgBox marks(i)
Next i
इससे हर element को loop के जरिए एक-एक करके access किया जा सकता है।