What is 2-Dimensional Array in VBA in Hindi
Makhanlal Chaturvedi University / BCA / VBA programming
2-Dimensional Array in VBA Explained with Examples in Hindi
What is 2-Dimensional Array in VBA in Hindi
Introduction of 2-Dimensional Array
जब हम Excel VBA में कई तरह के डेटा को एक साथ व्यवस्थित (organize) करके स्टोर करना चाहते हैं, तब हम Array का उपयोग करते हैं। अगर आपको एक ही प्रकार के डेटा को rows और columns के फॉर्म में स्टोर करना हो, तो उसके लिए 2-Dimensional Array (या 2D Array) का उपयोग किया जाता है।
एक 2D Array को आप Excel की worksheet के जैसे समझ सकते हैं, जिसमें rows और columns होते हैं। जैसे Excel में किसी cell को row और column नंबर से access किया जाता है, वैसे ही VBA में भी किसी element को दो indexes से access किया जाता है।
Structure of 2D Array
2D Array को "Matrix" या "Table" की तरह समझा जा सकता है। इसमें दो index होते हैं: पहला row को represent करता है और दूसरा column को। Syntax इस प्रकार होता है:
Dim arrayName(rowSize, columnSize) As DataType
उदाहरण:
Dim marks(2, 3) As Integer
इस उदाहरण में 3 rows (0 to 2) और 4 columns (0 to 3) होंगे। यानी total 3 × 4 = 12 elements store किए जा सकते हैं।
Declaring and Accessing 2D Array Elements in Hindi
Declaring 2D Array
- VBA में 2D Array को declare करते समय दो index दिए जाते हैं: एक row के लिए और एक column के लिए।
- By default, VBA arrays 0 से start होते हैं।
- अगर आप चाहें तो Option Base 1 लिखकर arrays को 1 से शुरू कर सकते हैं।
Option Base 1
Dim studentMarks(3, 2) As Integer
Accessing 2D Array Elements
2D Array के elements को access करने के लिए दोनों indexes को specify करना पड़ता है। जैसे:
studentMarks(1, 2) = 75
MsgBox studentMarks(1, 2)
इस code में, row 1 और column 2 की position पर value 75 assign की गई है और उसे MsgBox में display किया गया है।
Full Example
Sub Demo2DArray()
Dim marks(1 To 2, 1 To 3) As Integer
marks(1, 1) = 80
marks(1, 2) = 90
marks(1, 3) = 85
marks(2, 1) = 70
marks(2, 2) = 88
marks(2, 3) = 92
MsgBox "Student 2 Subject 3 Marks: " & marks(2, 3)
End Sub
Difference Between 1D and 2D Array in VBA in Hindi
Main Differences
| Feature | 1D Array | 2D Array |
|---|---|---|
| Structure | Single Row या Column | Rows और Columns दोनों |
| Indexing | 1 Index (e.g. arr(i)) | 2 Index (e.g. arr(i, j)) |
| Use Case | Simple List of Items | Tabular या Matrix Data |
| Example | Dim numbers(5) | Dim marks(2, 3) |
Explanation
1D Array का उपयोग तब किया जाता है जब आपको एक ही प्रकार की items की list बनानी हो। जैसे सिर्फ students के नाम या marks की list। लेकिन जब एक से ज्यादा categories को एक साथ जोड़ना हो — जैसे एक student के अलग-अलग subjects के marks — तब 2D Array का use करना चाहिए।
Using Nested Loops with 2-Dimensional Array in Hindi
Why Use Nested Loops?
जब आप 2D Array के हर element को individually read या write करना चाहते हैं, तो Nested Loop का उपयोग करना सबसे बेहतर होता है। Nested Loop का मतलब होता है – एक loop के अंदर दूसरा loop।
Structure of Nested Loop
Outer loop rows को represent करता है और inner loop columns को। उदाहरण देखें:
Sub Read2DArray()
Dim data(1 To 3, 1 To 2) As Integer
Dim i As Integer, j As Integer
' Values assign करना
For i = 1 To 3
For j = 1 To 2
data(i, j) = i * j
Next j
Next i
' Values print करना
For i = 1 To 3
For j = 1 To 2
Debug.Print "data(" & i & "," & j & ") = " & data(i, j)
Next j
Next i
End Sub
Real-Life Example
मान लीजिए आपके पास 3 students हैं और हर student के 3 subjects के marks हैं। आप इस डेटा को एक 2D Array में store कर सकते हैं और nested loop से उन्हें process कर सकते हैं।
Sub StudentMarks()
Dim marks(1 To 3, 1 To 3) As Integer
Dim i As Integer, j As Integer
' Assigning marks
For i = 1 To 3
For j = 1 To 3
marks(i, j) = InputBox("Enter marks of Student " & i & " for Subject " & j)
Next j
Next i
' Displaying marks
For i = 1 To 3
For j = 1 To 3
MsgBox "Marks of Student " & i & " in Subject " & j & " is: " & marks(i, j)
Next j
Next i
End Sub
Summary Points
- 2D Array का उपयोग तब किया जाता है जब डेटा rows और columns में हो।
- हर element को access करने के लिए दो indexes की जरूरत होती है।
- Nested loops से आप हर cell को efficiently process कर सकते हैं।
- यह beginners के लिए थोड़ा नया हो सकता है, लेकिन Excel sheet जैसा सोचें तो समझना आसान होगा।
FAQs
Dim arrayName(row, column) As DataTypeउदाहरण के लिए:
Dim marks(2, 3) As Integer जहाँ 3 rows और 4 columns होंगे।
MsgBox marks(1, 2)यह उस element को दिखाएगा जो row 1 और column 2 पर स्थित है।
arr(i), जबकि 2D Array में row और column दोनों के लिए दो index होते हैं जैसे arr(i, j)। 1D Arrays का उपयोग linear डेटा के लिए होता है, जबकि 2D Arrays का उपयोग tabular डेटा के लिए होता है।
For i = 1 To 3
For j = 1 To 2
MsgBox arr(i, j)
Next j
Next i
Option Base 1 लिखें।