Dictionary ऑब्जेक्ट

Dictionary ऑब्जेक्ट आपको कुंजी-मूल्य जोड़े के रूप में डेटा प्रबंधित करने देता है। VBA की Collection के विपरीत, Dictionary कुंजी अस्तित्व जांच, सभी कुंजियों/मूल्यों की पुनर्प्राप्ति, और सीधे मूल्य संशोधन प्रदान करता है।

Dictionary का उपयोग कब करें

  • जब नाम (कुंजी) द्वारा डेटा खोजना हो
  • जब कुंजी अस्तित्व की जांच करनी हो
  • जब डेटा की गिनती या समूहीकरण करना हो
  • जब अद्वितीय मूल्य फ़िल्टर करना हो

Dictionary बनाना

लेट बाइंडिंग (अनुशंसित)

dictionary_late_binding.bas
Sub DictionaryLateBanaye()
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    ' उपयोग के लिए तैयार
    Debug.Print TypeName(dict)  ' "Dictionary"
End Sub
チェック
लेट बाइंडिंग की सिफारिश की जाती है क्योंकि इसमें रेफरेंस सेटिंग की आवश्यकता नहीं होती।
チェック
Scripting.Dictionary केवल Windows पर उपलब्ध है और Mac के लिए Excel में काम नहीं करता। साथ ही, किसी अस्तित्वहीन कुंजी को पढ़ने पर त्रुटि नहीं आती, बल्कि खाली मान के साथ वह कुंजी जुड़ जाती है; जांचने के लिए हमेशा Exists का उपयोग करें।

बेसिक ऑपरेशंस

Add: आइटम जोड़ना

dictionary_add.bas
Sub DictionaryJodein()
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    ' कुंजी-मूल्य जोड़े जोड़ें
    dict.Add "E001", "अमित शर्मा"
    dict.Add "E002", "प्रिया गुप्ता"
    dict.Add "E003", "राज कुमार"

    Debug.Print dict.Count  ' 3
    Debug.Print dict("E001") ' "अमित शर्मा"
End Sub
チェック
डुप्लिकेट कुंजी जोड़ने से त्रुटि होगी। पहले Exists से जांचें।

Exists: कुंजी जांच

dictionary_exists.bas
Sub KunjiJanchein()
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    dict.Add "E001", "अमित शर्मा"

    If dict.Exists("E001") Then
        Debug.Print "कुंजी मौजूद है: " & dict("E001")
    Else
        Debug.Print "कुंजी नहीं मिली"
    End If
End Sub

Remove: आइटम हटाना

dictionary_remove.bas
Sub ItemHatayein()
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    dict.Add "E001", "अमित शर्मा"
    dict.Add "E002", "प्रिया गुप्ता"

    Debug.Print "हटाने से पहले: " & dict.Count  ' 2

    dict.Remove "E001"

    Debug.Print "हटाने के बाद: " & dict.Count  ' 1
End Sub

RemoveAll: सभी आइटम हटाना

dictionary_removeall.bas
Sub SabHatayein()
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    dict.Add "E001", "अमित शर्मा"
    dict.Add "E002", "प्रिया गुप्ता"

    dict.RemoveAll

    Debug.Print dict.Count  ' 0
End Sub

सभी आइटम में लूप करना

Keys और Items प्रॉपर्टी

dictionary_loop.bas
Sub DictionaryLoop()
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    dict.Add "E001", "अमित शर्मा"
    dict.Add "E002", "प्रिया गुप्ता"
    dict.Add "E003", "राज कुमार"

    ' सभी कुंजियों में लूप
    Dim key As Variant
    For Each key In dict.Keys
        Debug.Print key & ": " & dict(key)
    Next key
End Sub

प्रैक्टिकल उदाहरण

डेटा गिनती

dictionary_count.bas
Sub VibhaagGinein()
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    ' नमूना डेटा
    Dim data As Variant
    data = Array("बिक्री", "HR", "बिक्री", "IT", "HR", "बिक्री")

    Dim item As Variant
    For Each item In data
        If dict.Exists(item) Then
            dict(item) = dict(item) + 1
        Else
            dict.Add item, 1
        End If
    Next item

    ' परिणाम दिखाएं
    Dim key As Variant
    For Each key In dict.Keys
        Debug.Print key & ": " & dict(key)
    Next key
    ' बिक्री: 3
    ' HR: 2
    ' IT: 1
End Sub

अद्वितीय मूल्य फ़िल्टर करना

dictionary_unique.bas
Function AadwitiyaPrapt(rng As Range) As Variant
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    Dim cell As Range
    For Each cell In rng
        If Not dict.Exists(cell.Value) Then
            dict.Add cell.Value, True
        End If
    Next cell

    AadwitiyaPrapt = dict.Keys
End Function

Dictionary बनाम Collection

विशेषताDictionaryCollection
कुंजी जांचExists मेथडअसमर्थित
मूल्य अपडेटसीधा असाइनमेंटहटाएं और जोड़ें
सभी कुंजियांKeys मेथडअसमर्थित
सभी मूल्यItems मेथडअसमर्थित
आवश्यकताएंएक्सटर्नल संदर्भबिल्ट-इन

सारांश

  • Dictionary: कुंजी-मूल्य डेटा प्रबंधन के लिए उत्कृष्ट
  • Exists: डुप्लिकेट कुंजियों से बचने के लिए पहले जांचें
  • Keys/Items: सभी डेटा को आसानी से एक्सेस करें
  • लेट बाइंडिंग: रेफरेंस मुक्त उपयोग के लिए CreateObject का उपयोग करें

Dictionary जटिल डेटा प्रबंधन के लिए Collection से अधिक शक्तिशाली है।

#VBA #Dictionary #कुंजी-मूल्य #Excel