首页 > 解决方案 > 过滤条件仅获取数组中的第一个值

问题描述

我在工作表 3 上使用值列表,然后从 (strArray) 构建一个数组

' ADDS ALL ROWS FROM SHEET3 TO A ARRAY
With Worksheets(3)
TotalRows = .Range("A" & .Rows.Count).End(xlUp).Row
strArray = .Range("A1:A" & TotalRows).Value
MsgBox UBound(strArray)
'Debug.Print Join(strArray, ", ")
End With

在此之后,我尝试使用以下代码过滤我的数据:

With Worksheets(1)
    .Columns("A").AutoFilter Field:=1, Criteria1:=strArray, Operator:=xlFilterValues
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("A1:A" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy _
    Destination:=Sheets(2).Range("A1")
End With

发生的情况是它只过滤数组中的第一个值。但是,如果我将strArray更改为array("test", "test")例如,它可以正常工作并根据数组中的所有值构建标准。

为什么会出现这种情况?

完整代码:

Dim strArray As Variant
Dim TotalRows As Long
Dim LastRow As Long

' ADDS ALL ROWS FROM SHEET3 TO A ARRAY
With Worksheets(3)
TotalRows = .Range("A" & .Rows.Count).End(xlUp).Row
strArray = .Range("A1:A" & TotalRows).Value
MsgBox UBound(strArray)
'Debug.Print Join(strArray, ", ")
End With


With Worksheets(1)
    .Columns("A").AutoFilter Field:=1, Criteria1:=strArray, Operator:=xlFilterValues
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("A1:A" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy _
    Destination:=Sheets(2).Range("A1")
End With

标签: excelvba

解决方案


推荐阅读