我使用此公式将唯一记录从 A 列复制到 B 列。
Range("A1", Range("A100").End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("B1"), Unique:=True
您如何将过滤后的结果放入 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
这将使数组的元素 0 为空,但 UBound(SelectedData) 返回选择中的行数
以防万一有人再次看到这个......我创建了这个函数来处理一维范围,但它也会将更高维度的范围写入一维数组;修改以将多维范围写入“相同形状”数组应该不会太难。您需要引用 scrrun.dll 来创建字典对象。缩放可能是个问题,因为使用了“for each”循环,但如果您使用的是 EXCEL,您可能不必担心:
Function RangeToArrUnique(rng As Range)
Dim d As Object, cl As Range
Set d = CreateObject("Scripting.Dictionary")
For Each cl In rng
d(cl.Value) = 1
Next cl
RangeToArrUnique = d.keys
End Function
我已经以这种方式对此进行了测试:
Dim dat as worksheet
set dat = sheets("Data")
roomArr = Array("OR01","OR02","OR03")
dat.UsedRange.AutoFilter field:=2, criteria1:=roomArr, operator:=xlFilterValues
fltArr = RangeToArrUnique(dat.UsedRange.SpecialCells(CellTypeVisible))
希望这可以帮助那里的人!
你会想读这个,它会为你指明正确的方向
它说:
Sub tester()
Dim arr
arr = UniquesFromRange(ActiveSheet.Range("A1:A5"))
If UBound(arr) = -1 Then
Debug.Print "no values found"
Else
Debug.Print "got array of unique values"
End If
End Sub
Function UniquesFromRange(rng As Range)
Dim d As Object, c As Range, tmp
Set d = CreateObject("scripting.dictionary")
For Each c In rng.Cells
tmp = Trim(c.Value)
If Len(tmp) > 0 Then
If Not d.Exists(tmp) Then d.Add tmp, 1
End If
Next c
UniquesFromRange = d.keys
End Function
下面从 A 列获取信息并给出一个列表。它假设您有一个可用于数据输入的“Sheet3”(您可能希望更改它)。
Sub test()
Dim targetRng As Range
Dim i As Integer
Set targetRng = Sheets(3).Range("a1")
Range("A1", Range("A999").End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=targetRng, Unique:=True
Dim numbElements As Integer
numbElements = targetRng.End(xlDown).Row
Dim arr() As String
ReDim arr(1 To numbElements) As String
For i = 1 To numbElements
arr(i) = targetRng.Offset(i - 1, 0).Value
Next i
End Sub
将过滤范围存储在数组中的一种简单方法是使用复制粘贴技巧。创建一个工作表并将其隐藏或非常隐藏。说它的代号是sht_calc
。此函数将为您提供一个二维数组,除非您只有一列并且过滤的行只有一列,在这种情况下,它将是一个简单的变体变量而不是数组
Function GetArrayFromFilteredRange(rng As Range) As Variant
Dim arr As Variant
sht_calc.Cells.Clear
rng.Copy sht_calc.Range("A1")
arr = sht_calc.UsedRange.Value
GetArrayFromFilteredRange = arr
End Function
例如,如果您想在工作表中调用Table1
的表中获取过滤行的数组,代码名称为sht1
您可以简单地执行以下操作:
dim rng as range
arr = GetArrayFromFilteredRange(sht1.ListObjects("Table1").DataBodyRange.SpecialCells(xlCellTypeVisible))
arr=GetArrayFromFilteredRange(rng)
这是另一种方法。如果没有结果,它什么也不做。
Public Sub filteredRangeToArray(rg As Range, arr As Variant)
Dim i As Long
Dim j As Long
Dim row As Range
'If 0 results in Filter just exit
If Not rg.SpecialCells(xlCellTypeVisible).Count > 0 Then Exit Sub
i = 1
Erase arr
ReDim arr(1 To rg.Columns.Count, 1 To _
rg.Columns(1).SpecialCells(xlCellTypeVisible).Count)
For Each row In rg.Rows
If Not row.Hidden Then
For j = LBound(arr, 1) To UBound(arr, 1)
arr(j, i) = row.Cells(j)
Next j
i = i + 1
End If
Next row
arr = WorksheetFunction.Transpose(arr)
End Sub