excel 使用UniqueIDS.AddIfAbsent时出现运行时错误438

ztmd8pv5  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(97)

运行以下代码时,我在“If Not UniqueIDs.AddIfAbsent(ID)”行收到运行时错误438:经过一些研究,似乎UniqueIDs集合不支持Method/Property。我对所有这些都有点陌生,所以只是想谷歌一下所有这些术语。
我采取的一些步骤是将.AddIfAbsent替换为“If Not UniqueIDs.Exists(ID)Then”,并且不使用“ActiveSheet”,而是使用Dim ws As Worksheet语句。
我面临的另一个问题是最后一组代码,特别是“For Each ID in SortedUniqueIDs”的“ID”部分,我得到了一个编译错误:For每个控制变量必须是变量或对象”
我使用的代码如下:

Sub SumUniqueIdentifiers()
'Declare Variables
Dim ID As String
Dim Sum As Double
Dim LastRow As Long
Dim UniqueIDs As New Collection
Dim SortedUniqueIDs As Collection

Sheets("TH").Activate

'Set the last row of the data and Worksheet
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

'Initialize the sum
Sum = 0

'Loop through the data and sum the values for each unique ID
For i = 3 To LastRow
    ID = ActiveSheet.Cells(i, 1).Value

    'If the ID is new, add to the collection
    If Not UniqueIDs.AddIfAbsent(ID) Then
        Sum = 0
    End If

   'Add the value in the next column to the sum
    Sum = Sum + Sheets("Sheet1").Cells(i, 2).Value

    'Store the sum in the collection
     UniqueIDs(ID) = Sum
 Next I

'Loop through the collection and print the sums
 For Each ID In SortedUniqueIDs
    ActiveSheet.Cells(1, 6).Value = ID
    ActiveSheet.Cells(LastRow + 1, 7).Value = SortedUniqueIDs(ID)
    LastRow = LastRow + 1
Next ID

End Sub

字符串
我采取的一些步骤是将.AddIfAbsent替换为“If Not UniqueIDs.Exists(ID)Then”,并且不使用“ActiveSheet”,而是使用Dim ws As Worksheet语句。
目标是总结唯一标识符集合如果我在列A中有一系列唯一标识符,每个都有多个条目,我想使用列D中的数字总结每个唯一标识符,并将它们打印在列F中,由每个唯一标识符总结

kpbwa7wx

kpbwa7wx1#

最快,但非常简单的方法(取决于进一步的使用)
在列F中选择从row 1row ?的单元格,然后在第一个单元格(?=列A中最后使用的单元格行号)

=SUMIF(A1:A?,"=" & A1:A?,D1:D?)

字符串
然后按CTRL+SHIFT+ENTER(不在365中)
在列F中的每一行中,将示出相应标识符的值的总和。

jljoyd4f

jljoyd4f2#

Collection对象不支持AddIfAbsent。使用Dictionary对象代替。

Sub SumUniqueIdentifiers()
    'Declare Variables
    Dim ID As String, i
    Dim Sum As Double
    Dim LastRow As Long, LastR
    '    Dim UniqueIDs As New Collection
    '    Dim SortedUniqueIDs As Collection
    Dim dataR As Range
    Dim UniqueIDs As Object
    Set UniqueIDs = CreateObject("scripting.dictionary")
    Sheets("TH").Activate
    'Set the last row of the data and Worksheet
    LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    'Initialize the sum
    Sum = 0
    'Loop through the data and sum the values for each unique ID
    For i = 3 To LastRow
        ID = ActiveSheet.Cells(i, 1).Value
        'If the ID is new, add to the dict
        If Not UniqueIDs.exists(ID) Then
            UniqueIDs(ID) = 0
        Else
            UniqueIDs(ID) = UniqueIDs(ID) + Sheets("Sheet1").Cells(i, 2).Value
        End If
    Next i
    'Loop through the collection and print the sums
    LastR = LastRow + 1
    For Each ID In UniqueIDs
        ActiveSheet.Cells(LastR, 6).Value = ID
        ActiveSheet.Cells(LastR + 1, 7).Value = SortedUniqueIDs(ID)
        LastR = LastR + 1
    Next ID
    Set dataR = Range(Cells(LastRow + 1, 6), Cells(LastR - 1, 7))
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add Key:=dataR.Columns(1), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange dataR
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .Apply
    End With
End Sub

字符串

相关问题