Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88835

Take the value of a label from a dictionary and separate it into different variables

$
0
0

I'm working with Excel VBA and I have a dictionnary called "dico" who contains 3 keys with differents values, like this example :

Dico 2

Sum for 'Fonctionnel' is a1 b2 c3 a4 b5 c6 a7 b8 c9 a10 b11 c12 a19 b20 c21 a22 b23 c24

Sum for 'Securite' is a13 b14 c15 a16 b17 c18 a31 b32 c33 a34 b35 c36

Sum for 'Technique' is a25 b26 c27 a28 b29 c30

I want for each keys, to have 3 variables regrouping the values with the same type, for example : I want to have for Fonctionnel :

allA = a1 a4 a7 a10 a19 a22
allB = b2 b5 b8 b11 b20 b23
allC = c3 c6 c9 c12 c21 c24

For the moment, I have this code :

Sub groupByTypo()
    Dim rng As Range, c As Range, dict, v, k

    Set dict = CreateObject("scripting.dictionary")
    Set dict2 = CreateObject("scripting.dictionary")

    'get the input range for the labels
    With ActiveSheet
        Set rng = .Range(.Range("C1"), .Cells(.Rows.Count, 3).End(xlUp))
    End With

    For Each c In rng.Cells
        v = Trim(c.Value)
        'if there's a label, add to the count
        If Len(v) > 0 Then dict(v) = dict(v) + c.Offset(0, -1).Value
        If Len(v) > 0 Then dict2(v) = dict2(v) + c.Offset(0, -2).Value
    Next c

    'output the counts
    Debug.Print "Dico 1"
    For Each k In dict
        Debug.Print "Sum for '"& k & "' is "& dict(k)
    Next k

    Debug.Print "Dico 2"
    For Each k In dict2
        Debug.Print "Sum for '"& k & "' is "& dict2(k)
    Next k

End Sub

And I have this Excel file :

excel file

Thank you for your help !


Viewing all articles
Browse latest Browse all 88835

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>