我使用这个公式将唯一记录从A列复制到B列.
Range("A1", Range("A100").End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("B1"), Unique:=True
Run Code Online (Sandbox Code Playgroud)
如何将过滤后的结果放入Excel VBA中的数组中,而不是将其复制到B列中?
自问这个问题已经过去了一年,但我今天遇到了同样的问题,这是我的解决方案:
Function copyFilteredData() As Variant
Dim selectedData() As Variant
Dim aCnt As Long
Dim rCnt As Long
Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Select
On Error GoTo MakeArray:
For aCnt = 1 To Selection.Areas.Count
For rCnt = 1 To Selection.Areas(aCnt).Rows.Count
ReDim Preserve SelectedData(UBound(selectedData) + 1)
selectedData(UBound(selectedData)) = Selection.Areas(aCnt).Rows(rCnt)
Next
Next
copyFilteredData = selectedData
Exit Function
MakeArray:
ReDim selectedData(1)
Resume Next
End Function
Run Code Online (Sandbox Code Playgroud)
这将使数组的元素0为空,但UBound(SelectedData)返回选择中的行数