0

我在 Sheet1,A 列中有一个未排序的名称列表。其中许多名称在列表中出现不止一次。

在 Sheet2 Column AI 上,需要一个按字母顺序排序的名称列表,没有重复值。

使用 VBA 实现这一目标的最佳方法是什么?

到目前为止我见过的方法包括:

  1. 以 CStr(name) 为键创建一个集合,遍历范围并尝试添加每个名称;如果有错误,它不是唯一的,忽略它,否则将范围扩大 1 个单元格并添加名称
  2. 与 (1) 相同,但忽略错误。循环完成后,集合中只有唯一值:然后将整个集合添加到范围中
  3. 在范围上使用匹配工作表功能:如果不匹配,则将范围扩大一个单元格并添加名称
  4. 也许对数据选项卡上的“删除重复项”按钮进行一些模拟?(没看过这个)
4

2 回答 2

2

我真的很喜欢 VBA 中的字典对象。它不是本机可用的,但功能非常强大。您需要添加一个引用,Microsoft Scripting Runtime然后您可以执行以下操作:

Dim dic As Dictionary
Set dic = New Dictionary
Dim srcRng As Range
Dim lastRow As Integer

Dim ws As Worksheet
Set ws = Sheets("Sheet1")

lastRow = ws.Cells(1, 1).End(xlDown).Row
Set srcRng = ws.Range(ws.Cells(1, 1), ws.Cells(lastRow, 1))

Dim cell As Range

For Each cell In srcRng
    If Not dic.Exists(cell.Value) Then
        dic.Add cell.Value, cell.Value   'key, value
    End If
Next cell

Set ws = Sheets("Sheet2")    

Dim destRow As Integer
destRow = 1
Dim entry As Variant

'the Transpose function is essential otherwise the first key is repeated in the vertically oriented range
ws.Range(ws.Cells(destRow, 1), ws.Cells(dic.Count, 1)) = Application.Transpose(dic.Items)
于 2012-05-10T01:37:18.357 回答
0

正如您所建议的,某种字典是关键。我会使用一个 Collection - 它是内置的(与 Scripting.Dictionary 相反)并且可以完成这项工作。

如果“最佳”是指“快速”,第二个技巧是不要单独访问每个单元格。而是使用缓冲区。即使有数千行输入,下面的代码也会很快。

代码:

' src is the range to scan. It must be a single rectangular range (no multiselect).
' dst gives the offset where to paste. Should be a single cell.
' Pasted values will have shape N rows x 1 column, with unknown N.
' src and dst can be in different Worksheets or Workbooks.
Public Sub unique(src As Range, dst As Range)
    Dim cl As Collection
    Dim buf_in() As Variant
    Dim buf_out() As Variant
    Dim val As Variant
    Dim i As Long

    ' It is good practice to catch special cases.
    If src.Cells.Count = 1 Then
        dst.Value = src.Value   ' ...which is not an array for a single cell
        Exit Sub
    End If
    ' read all values at once
    buf_in = src.Value
    Set cl = New Collection
    ' Skip all already-present or invalid values
    On Error Resume Next
    For Each val In buf_in
        cl.Add val, CStr(val)
    Next
    On Error GoTo 0

    ' transfer into output buffer
    ReDim buf_out(1 To cl.Count, 1 To 1)
    For i = 1 To cl.Count
        buf_out(i, 1) = cl(i)
    Next

    ' write all values at once
    dst.Resize(cl.Count, 1).Value = buf_out

End Sub
于 2012-05-10T07:33:30.017 回答