Notes in Hindi

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

VBA में 2-Dimensional Array एक ऐसा डेटा structure होता है जिसमें डेटा को row और column के रूप में store किया जाता है। इसे आप Excel की worksheet की तरह समझ सकते हैं जिसमें हर element को दो indexes (row, column) से access किया जाता है।
2D Array को declare करने के लिए आप नीचे दिए गए syntax का उपयोग कर सकते हैं:
Dim arrayName(row, column) As DataType
उदाहरण के लिए: Dim marks(2, 3) As Integer जहाँ 3 rows और 4 columns होंगे।
किसी भी 2D Array के element को access करने के लिए row और column दोनों index की आवश्यकता होती है। उदाहरण:
MsgBox marks(1, 2)
यह उस element को दिखाएगा जो row 1 और column 2 पर स्थित है।
1D Array एक simple list होती है जिसमें केवल एक index होता है, जैसे arr(i), जबकि 2D Array में row और column दोनों के लिए दो index होते हैं जैसे arr(i, j)। 1D Arrays का उपयोग linear डेटा के लिए होता है, जबकि 2D Arrays का उपयोग tabular डेटा के लिए होता है।
Nested Loops का उपयोग तब किया जाता है जब आपको 2D Array के हर element पर individually काम करना हो। Outer loop rows को represent करता है और inner loop columns को:
For i = 1 To 3
  For j = 1 To 2
    MsgBox arr(i, j)
  Next j
Next i
Option Base statement यह तय करता है कि Array के index 0 से शुरू होंगे या 1 से। Default रूप से VBA में Arrays का index 0 से शुरू होता है। यदि आप चाहते हैं कि index 1 से शुरू हो तो code में सबसे ऊपर Option Base 1 लिखें।

Please Give Us Feedback