利用EXCEL VBA如何将一列数据中不重复的数据读取到数组中

2024-11-04 19:15:53
推荐回答(1个)
回答(1):

'利用字典去重,下面是示例代码,去除第一列的重复项Sub Test() '利用字典去重,字典的特性是key值不能重复
Dim Dic, Arr
Dim i As Integer, r As Integer
Dim Str As String
r = Sheet1.Range("A65536").End(xlUp).Row
If r = 1 Then Exit Sub '如果第一列没有数据那么退出程序
Set Dic = CreateObject("scripting.dictionary") '创建字典对象
For i = 1 To r '将第一列数据添加到字典的key值中
Dic(CStr(Cells(i, 1))) = ""
Next
Arr = Dic.keys '返回字典key的数组
Set Dic = Nothing '销毁对象
Str = Join(Arr, ",") '将数组中的内容显示为一字符串
MsgBox Str
End Sub