Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

vba - VBA更改字典值(VBA change dictionary value)

Im trying to change values in a dictionary dynamically.

(我试图动态改变字典中的值。)

If value exists in dictionary, change that value to dictionary value + new value (incremental).

(如果字典中存在值,则将该值更改为字典值+新值(递增)。)

Im unable to do this however, i get the Run-time error 451: Property let procedure not defined and property get procedure did not return an object.

(我无法执行此操作,但是我收到运行时错误451:属性让过程未定义,属性获取过程未返回对象。)

Can someone help me do a "sumifs" -type of changes to the dictionary?

(有人可以帮我做一个“ sumifs”类型的字典更改吗?)

Sub Sumifs()

Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
Dim arr As Variant
Dim lr1 As Long
Dim arr2 As Variant
Dim lr2 As Long

With Blad15
        lr1 = Worksheets("Sheet1").Cells(.Rows.Count, 5).End(xlUp).Row
        arr = Worksheets("Sheet1").Range("E20:E" & lr1)
        Debug.Print UBound(arr)
        Debug.Print lr1
End With

ThisWorkbook.Sheets("Sheet1").Select
For i = 1 To UBound(arr)
    objDictionary.Add Key:=CStr(Cells(i + 19, 5)), Item:=CStr(Cells(i + 19, 5))
Next

ThisWorkbook.Sheets("Sheet2").Select

    With Blad6
        lr2 = Worksheets("Sheet2").Cells(.Rows.Count, 2).End(xlUp).Row
        arr2 = Worksheets("Sheet2").Range("B2:B" & lr2 + 1)

    End With

For i = 1 To UBound(arr)
    If objDictionary.Exists(Cells(i + 1, 2).Value) Then
        objDictionary(Cells(i + 1, 2).Value) = objDictionary.Items(Cells(i + 1, 2)) + Worksheets("Sheet2").Cells(i + 1, 8).Value 'Error occurs here
    End If

Next


End Sub

在此处输入图片说明

  ask by Carlsberg789 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...