字典添加(如果不存在) [英] Dictionary add if doesn't exist

查看:136
本文介绍了字典添加(如果不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含重复项的数据集,想遍历数据并在键不存在的情况下添加.

I have a data set with duplicates and want to loop through the data and add if the key doesn't exist.

Dim SSBIds As New Scripting.Dictionary
Dim key As Variant
For i = 2 To endSSB
    For Each key In SSBIds
        If Not SSBIds.Exists(key) Then
            SSBIds.Add SSB.Cells(i, 1).Value2, i
        End If
    Next
Next i

endSSB只是数据集的最后一行.我只是一直盯着它,不知道为什么它不起作用.

endSSB is just the last row of the data set. I just keep staring at this and can't figure out why it won't work.

推荐答案

使用:

`Dim SSBIds As New Scripting.Dictionary`

您要定义一个全新的Dictionary,然后使用:

you are defining a brand new Dictionary, and then with:

`For Each key In SSBIds`

您正在尝试遍历其键,但是这些键是空的...

you are trying to loop through its keys, which however are empty...

也许你在追求这个

Dim SSBIds As New Scripting.Dictionary
Dim i As Long
With SSB
    For i = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
        If Not SSBIds.Exists(.Cells(i, 1).Value2) Then SSBIds.Add .Cells(i, 1).Value2, i
    Next
End With

这篇关于字典添加(如果不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆