|
提取唯一值功能在超过10万以上数据时运行会导致Excel无法响应,本人此前也有过这方面的求助,建议采用灰袍法师写的代码思路。
详见本人的帖子:
请教各位超过60万数据提取不重复值问题?【已解决】
http://club.excelhome.net/thread-892294-1-1.html
- Sub remove_duplicate_by_sortting()
- Dim i As Long, j As Long, k As Long, count As Long, unique As Long
- Dim key, t, data_in(), arr_sort()
- t = Timer
- Sheets(2).Range("A1").Resize(Rows.count, Columns.count).ClearContents
- Sheets(1).Select
- Randomize
- r = 65536
- c = 13 '一共十三列数据
- data_in = Range("A1").Resize(r, c).Value '读取65536x13的单元格区域
- ReDim arr_sort(1 To r * c)
- count = 0
- For i = 1 To c
- For j = 1 To r
- key = CStr(data_in(j, i))
- If key <> "" Then
- count = count + 1
- arr_sort(count) = key
- End If
- Next j
- Next i
- Erase data_in
- Call QuickSort(arr_sort, 1, count)
- ReDim arr_out(1 To 65536, 1 To 1)
- key = ""
- j = 0
- k = 1
- For i = 1 To count
- If arr_sort(i) <> key Then
- unique = unique + 1
- key = arr_sort(i)
- j = j + 1
- arr_out(j, 1) = key
- If j = 65536 Then
- Sheets(2).Cells(1, k).Resize(65536, 1).Value = arr_out
- ReDim arr_out(1 To 65536, 1 To 1)
- j = 0
- k = k + 1
- End If
- End If
- Next i
- Sheets(2).Cells(1, k).Resize(j, 1).Value = arr_out
-
- MsgBox Format(Timer - t, "0.000") & " 秒 " & unique & " 个不重复值"
- End Sub
- Sub QuickSort(Arr_In(), L As Long, r As Long)
- Dim i As Long, j As Long, k As Long, a As Long, b As Long, c As Long
- Dim Pivot, Swap, Insert
- i = L
- j = r
- If r - L <= 12 Then
- For b = L To r
- Insert = Arr_In(b)
- For c = b - 1 To L Step -1
- If Insert < Arr_In(c) Then
- Arr_In(c + 1) = Arr_In(c)
- Arr_In(c) = Insert
- Else
- Exit For
- End If
- Next c
- Next b
- Else
- Pivot = Arr_In(Int(Rnd * (r - L)) + L)
- While (i < j)
- For a = i To r
- If Arr_In(a) >= Pivot Then Exit For
- Next a
- i = a
- For b = j To L Step -1
- If Arr_In(b) <= Pivot Then Exit For
- Next b
- j = b
- If (a < b) Then
- Swap = Arr_In(a)
- Arr_In(a) = Arr_In(b)
- Arr_In(b) = Swap
- i = i + 1
- j = j - 1
- End If
- Wend
- If (L < j) Then Call QuickSort(Arr_In, L, j)
- If (i < r) Then Call QuickSort(Arr_In, i, r)
- End If
- End Sub
复制代码
该贴已经同步到 roc.jame的微博 |
|