PDA

View Full Version : scripting.dictionary method



idnoidno
06-28-2017, 08:09 PM
19625

I put two columns of data(the first and second columns) into a set of variables(the three column), and then use the scripting.dictionary method to read the variables to generate the dictionary.keys, and then use the split function to separate the variables.But I have some question want to ask,
1) How to separate the variables into their original patterns, and then write them in columns 5 and 6
2)Why
sub t4 can work


mykey = d.keys
For i = 0 To UBound(mykey)
brr = Split(mykey(i), "_")
Cells(i + 1, 6) = brr(0)
Cells(i + 1, 7) = brr(1)
Next i


,but subt5 cannot


For i = 0 To d.Count
brr = Split(d.keys, "_")
Cells(i + 1, 6) = brr(0)
Cells(i + 1, 7) = brr(1)









Sub t4()
Dim d As Object
Dim i As Long
Dim arr, brr, s, mykey
Set d = CreateObject("scripting.dictionary")
arr = [a1].CurrentRegion
For i = 2 To UBound(arr)
s = arr(i, 1) & "_" & arr(i, 2)
If Not d.exists(s) Then
d(s) = arr(i, 3)
Else
d(s) = d(s) + arr(i, 3)
End If
Next i
mykey = d.keys
For i = 0 To UBound(mykey)
brr = Split(mykey(i), "_")
Cells(i + 1, 6) = brr(0)
Cells(i + 1, 7) = brr(1)
Next i
[h1].Resize(d.Count) = Application.Transpose(d.items)
Set d = Nothing
End Sub


Sub t5()
Dim d As Object
Dim i As Long
Dim arr, brr, s, mykey
Set d = CreateObject("scripting.dictionary")
arr = [a1].CurrentRegion


For i = 2 To UBound(arr)
s = arr(i, 1) & "_" & arr(i, 2)
If Not d.exists(s) Then
d(s) = arr(i, 3)
Else
d(s) = d(s) + arr(i, 3)
End If
Next i
For i = 0 To d.Count
brr = Split(d.keys, "_")
Cells(i + 1, 6) = brr(0)
Cells(i + 1, 7) = brr(1)
Next i
[h1].Resize(d.Count) = Application.Transpose(d.items)
Set d = Nothing
End Sub

p45cal
06-29-2017, 02:40 AM
For goodness' sake supply a file, not a picure of one! Why make people work to help you? Besides it's easier for you too, to supply a file than to make pictures.
For what you seem to be doing, there's likely a very easy solution, but it depends on whether this is a dictionary academic exercise or not.

mdmackillop
06-29-2017, 04:39 AM
You have been asked previously to provide proper samples and to use code tags. Your questions should also have meaningful titles. "Please help" does not qualify. If you cannot comply, future posts may be locked or deleted. I refer you again to our FAQ (http://www.vbaexpress.com/forum/faq.php)

mana
06-29-2017, 04:42 AM
For i = 0 To d.Count - 1
brr = Split(d.keys()(i), "_")

mana
06-29-2017, 06:02 AM
Option Explicit


Sub t6()
Dim d As Object
Dim i As Long
Dim arr, s, v()

Set d = CreateObject("scripting.dictionary")

arr = [a1].CurrentRegion.Value
ReDim v(UBound(arr), 1 To 3)

For i = 2 To UBound(arr)
s = arr(i, 1) & "_" & arr(i, 2)
If Not d.exists(s) Then
d(s) = d.Count
v(d(s), 1) = arr(i, 1)
v(d(s), 2) = arr(i, 2)
End If
v(d(s), 3) = v(d(s), 3) + arr(i, 3)
Next

[f1].Resize(d.Count, 3).Value = v

Set d = Nothing

End Sub

idnoidno
06-29-2017, 05:35 PM
I am so sorry to make mistake again.I will remember the rule.
Thanks Mr.mdmackillop's remider.