Scripting dictionary vba

Madhukar37

New member
Joined
Oct 15, 2018
Messages
3
Reaction score
0
Points
0
Excel Version(s)
2016
[FONT=&quot]i would like to code for following data in "data" worksheet with vba scripting dictionary, with names as key and marks as values, also output dictionary data to "output"worksheet of same workbook.[/FONT]
| Name | maths | science | lang | history |
|--------|-------|---------|------|---------|
| pandit | 78 | 78 | 57 | 67 |
| sushil | 44 | 70 | 67 | 79 |
| kiran | 48 | 55 | 57 | 67 |
| manoj | 78 | 79 | 67 | 69 |
| kumar | 78 | 52 | 80 | 74 |
| ranjit | 56 | 61 | 63 | 57
 
Attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.


Remember to desensitize the data.
 
Code:
' clsName Class Module Code
Public Name As String
Public maths As Long
Public science As Long
Public lang As Long
Public history As Long
 
 


' Standard module Code
Sub MakeDict()


    Dim dict As Dictionary
    
    ' Read the data to the dictionary
    Set dict = MultipleValues
    
    
    ' Write the Dictionary contents to a worksheet
    WriteToWorksheet dict, ThisWorkbook.Worksheets("Output")


End Sub


Private Function MultipleValues() As Dictionary


    ' Declare and create the Dictionary
    Dim dict As New Dictionary
    
    ' Get the worksheet
    Dim sh As Worksheet
    Set sh = ThisWorkbook.Worksheets("data")
    
    ' Get the range of all the adjacent data using CurrentRegion
    Dim rg As Range
    Set rg = sh.Range("A1").CurrentRegion


    Dim oNam As clsName, i As Long
    ' read through the data
    For i = 2 To rg.Rows.Count
    
        
        Set oNam = New clsName
        
        ' Set the values
        oNam.Name = rg.Cells(i, 1).Value
        oNam.Maths = rg.Cells(i, 2).Value
        oNam.science = rg.Cells(i, 3).Value
        oNam.lang = rg.cells(i, 4).Value
         oNam.history =rg.cells(i, 5).Value
        
       
        dict.Add oNam.Name, oNam
            
    Next i
    
    ' Return the dictionary to the Main sub
    Set MultipleValues = dict


End Function


' Write the Dictionary contents  to a worksheet
Private Sub WriteToWorksheet(dict As Dictionary, sh As Worksheet)
    
    ' Delete all existing data from the worksheet
    sh.Cells.ClearContents
    
    Dim row As Long
    row = 1
    
    Dim key As Variant, oNam As clsName
    ' Read through the dictionary
    For Each key In dict.Keys
        Set oNam = dict(key)
        With oNam
            ' Write out the values
            sh.Cells(row, 1).Value = .Name
            sh.Cells(row, 2).Value = .Maths
            sh.Cells(row, 3).Value = .science
            sh.Cells(row, 4).Value = .lang
            sh.Cells(row, 5).Value = .history
            row = row + 1
        End With
        
    Next key
End Sub

Capture2.PNGCapture2.PNGCapture2.PNGCapture2.PNGCapture2.PNGCapture2.PNGCapture2.PNG

expeted output is
something like this
Capture3.PNG
 
Last edited by a moderator:
scripting dictionary

i have attached relevent excel attachment
 

Attachments

  • Book1.xlsx
    11.4 KB · Views: 17
Back
Top