如何遍历一列并仅挑选唯一的整数?

时间:2018-12-18 03:07:45

标签: excel vba

我是VBA的新手,我正在尝试计算此列中唯一编号的数量。我想使用某种形式的列表来存储可见的数字,然后只添加尚未被看到的数字,然后获得该列表的长度,但是VBA似乎只有一定长度的数组。有见识吗?

列看起来像这样:
column looks like this

1 个答案:

答案 0 :(得分:0)

非VBA简易选项:

{=SUM(1/(COUNTIF(A1:A17,A1:A17)))}

enter image description here

要通过CTRL + SHIFT + ENTER验证的数组公式

VBA简易选项:

Option Explicit

Sub count_unique()

    Dim D1 As Object: Set D1 = CreateObject("scripting.dictionary")
    Dim R0 As Range
    Dim R1 As Range: Set R1 = Range("A1:A17") 'adapt to fit your range

    For Each R0 In R1: D1(R0.Value) = R0.Value: Next R0
    MsgBox D1.Count

End Sub

enter image description here