Ok, so I have a table of names in one column and corresponding numbers in another. Most names appear more than once and each time with a different number. The table is likely to be added to in the future. I'm trying to write a VBA macro that will output each name once and the total sum of the numbers attached to them on a separate sheet. I haven't used VBA in like 8 months and I'm really rusty. Suggestions?
remember to add the reference microsoft scripting runtime, i believe. u would need to use dictionary here.
Sub test()
Dim var As Variant
Dim rng As Range
Set rng = Range("A1:A100")
Dim dico As Dictionary
Set dico = New Dictionary
For Each var In rng.Cells
if dico.Exists(var.value) Then
dico(var.value) = dico(var.value) + var.offset(0,1).value
else
dico.Add var.value, var.offset(0,1).value
end if
Next var
Set rng = Range("C1")
Dim i as double
i = 0
For Each var In dico.keys
rng.offset(i).value = var
rng.offset(i,1).value = dico(var)
Next var
End sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.