1

Alright, maybe I've just been looking at this for too long but I keep getting an expected expression error, which I believe is just a syntax issue but I'm not entirely sure. I'm looping through one sheet and adding unique values of one column as keys to the dictionary while adding the numbers that correspond to the keys together. For example, if I have:

A 2
B 3
B 4
A 5
C 6

I want the dictionary to look like:

A 7
B 7
C 6

Here's my code, any help is appreciated.

Sub Name()
    Dim rng As Range
    Dim x As Integer
    Dim ranga As String
    Dim dico As Dictionary
    Set dico = New Dictionary
    Dim var As Variant
    Dim lastrow As Integer
    With Worksheets("Sheet1")
        lastrow = Range("A" & .Rows.Count).End(xlUp).Row
        ranga = "C6" & ":" & "C" & CStr(lastrow)
        Set rng = Range(ranga)
        For Each var In rng.Cells
            If dico.Exists(var.Value) Then
                dico(var.Value) = dico(var.Value) + var.Offset(0, 4).Value
            Else
                dico.add var.Value, var.Offset(0, 4).Value
            End If
        Next var
    End With

    With Worksheets("Sheet2")
        Set rng = Range("A2")
        Dim i As Integer
        i = 0
        For Each var In dico.Keys
            rng.Offset(i).Value = var
            rng.Offset(i, 1).Value = dico(var)
        Next var
    End With

End Sub
3
  • quick question: do you really need VBA for this? It would be more efficient to actually use built in tools and then load the data to a dictionary if still needs to be in a dictionary Commented Aug 6, 2014 at 12:51
  • Yeah, the table is constantly changing so even if I have static functions on the second sheet they wouldn't include new values. Commented Aug 6, 2014 at 12:53
  • I think you need i = i + 1 just before the Next var Commented Jun 11, 2018 at 19:16

2 Answers 2

3

I am new to stackoverflow, so I am a but unsure of the appropriate etiquette, but here is a working solution.

Public Sub dict_counter()
    Dim counter As New Dictionary
    Dim key As Range: Set key = ThisWorkbook.Sheets("sheet1").Range("A1")
    While Not IsEmpty(key)
        If counter.Exists(key.Value) Then
            counter(key.Value) = counter(key.Value) + key.Offset(ColumnOffset:=1)
        Else
            counter(key.Value) = key.Offset(ColumnOffset:=1)
        End If
        Set key = key.Offset(RowOffset:=1)
    Wend

    'Obviously you can output the dict contents to whatever location
    'is convenient

    Dim k As Variant
    For Each k In counter
        Debug.Print k; counter(k)
    Next k
End Sub
Sign up to request clarification or add additional context in comments.

Comments

2

Instead of

dico(var.Value) = dico(var.Value) + var.Offset(0, 4).Value

It should be

dico.Item(var.Value) = dico(var.Value) + var.Offset(0, 4).Value

See MSDN

Also, if you use With, you have to actually put leading .'s where you want to use methods or properties of it like this:

With Worksheets("Sheet1")
    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
End With

See MSDN

4 Comments

hmmm. Maybe that was part of the problem, but I still get the same error.
At what line does the code break and what is the exact error message you are getting?
It doesn't give me a line oddly enough, it just says compile error, expected: expression
I figured out that it was due to calling the sub "name".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.