【发布时间】:2021-03-13 03:50:46
【问题描述】:
我正在努力完成以下工作。
- 过滤列 A
- 从 B 列获取唯一值
所以给出下表...
我想过滤 A 列中的“One”,然后得到一个可以像这样粘贴到 C 列的数组...
我尝试过使用字典,但对它的工作原理知之甚少。可能有数千行,因此速度可能是一个问题,如果没有必要,我宁愿不要循环遍历每一行。
我见过使用高级过滤器恢复列的唯一值的解决方案,但从来没有过滤一列,然后使用过滤后的结果来获取唯一值列表。
我尝试过的代码示例(部分):
On Error Resume Next
enterpriseReportSht.ShowAllData
On Error GoTo 0
With enterpriseReportSht
.AutoFilterMode = False
With .Range(Cells(1, 1).Address, Cells(entRptLR, entRptLC).Address)
.AutoFilter Field:=manLevel2CN, Criteria1:=userInputsArr(i, manLevel2InputCN)
'.SpecialCells(xlCellTypeVisible).Copy Destination:=resultsSht.Range("A1")
End With
End With
filteredColArr = enterpriseReportSht.UsedRange.columns(manLevel4CN).Value
RemoveDuplicatesFromArray (filteredColArr)
使用此功能:
Public Function RemoveDuplicatesFromArray(sourceArray As Variant)
Dim duplicateFound As Boolean
Dim arrayIndex As Integer, i As Integer, j As Integer
Dim deduplicatedArray() As Variant
arrayIndex = -1
deduplicatedArray = Array(1)
For i = LBound(sourceArray) To UBound(sourceArray)
duplicateFound = False
For j = LBound(deduplicatedArray) To UBound(deduplicatedArray)
If sourceArray(i) = deduplicatedArray(j) Then
duplicateFound = True
Exit For
End If
Next j
If duplicateFound = False Then
arrayIndex = arrayIndex + 1
ReDim Preserve deduplicatedArray(arrayIndex)
deduplicatedArray(arrayIndex) = sourceArray(i)
End If
Next i
RemoveDuplicatesFromArray = deduplicatedArray
End Function
我担心的是它没有抓取过滤后的数据。我相信它抓住了一切。我也收到了删除重复功能的错误。
【问题讨论】:
-
什么版本的 Excel?如果您有 Office 365,则可以使用工作表公式执行此操作。