當前位置:成語大全網 - 新華字典 - 如何利用EXCEL VBA將壹列數據中不重復的數據讀取到數組中?

如何利用EXCEL VBA將壹列數據中不重復的數據讀取到數組中?

利用字典去重,下面是示例代碼,去除第壹列的重復項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