非连续数组作为xlfiltervalues的过滤条件

时间:2017-11-16 15:37:21

标签: arrays excel vba filter criteria

常见问题,我已经找到了我找到的所有答案,并最终使它几乎正常工作。

我有一个折扣选项列表,我们称之为名为范围F,下调1列。 用户过滤掉他们不想申请的折扣。 我需要根据用户的选择进行不过滤,做工作和重新过滤。

我创建了一个只有可见单元格的数组,循环和范围联合。这可以正常工作,但通常会生成一个非连续的数组。

当我运行它时,我没有收到错误。但是,连续数组中断点下方的条目不会被重新过滤。

刚刚意识到这是一个不喜欢非连续数组的转置 - 仍然需要帮助而且无疑其他人也有同样的问题,所以留下来就是

什么是最简单,最无痛(几乎是星期五),说服Criteria1在我的非连续数组中包含最后一个元素的方法?

Sub Filters()

'Dimension variables
Dim Rng As Range
Dim i, Lim As Integer
Dim w As Worksheet
Dim Op As Variant

Set w = ActiveSheet

'Set Lim as total number of rows in named range "F" (only 1 cell in use but same effect)
Lim = Range("F").Rows.Count

'Data has header row so skip to row 2
i = 2

'Loop through i up to limit
Do While i <= Lim
    'If the row is not hidden by the filters the user chose

    If Range("F")(i, 1).EntireRow.Hidden = False Then
        'Check if the range is nothing - if it is, union will not work to itself
        'Union requires non-empty arguments

        If Rng Is Nothing Then
            'Set the Rng to include the current cell from "F"
            Set Rng = Range("F")(i, 1)

        Else
            'If Rng has some value, add the current cell to it by Union
            Set Rng = Application.Union(Rng, Range("F")(i, 1))

        End If

    End If

    'Increment i
    i = i + 1

    Loop

    If w.AutoFilter.Filters.Item(1).Operator <> False Then Op = w.AutoFilter.Filters.Item(1).Operator

    'This gives the correct range, but most often non-contiguous
    MsgBox Range("F").Address

    'Remove AutoFilter
    w.AutoFilterMode = False




    'Insert Code Here




    'Put filters back

    'Check for Rng being non-empty (pointless running code if it is)
    If Not IsEmpty(Rng) Then
        'If there is an operator then use the array
        If Op Then
            'Found this option useful here - can transpose the array values which generates an array Criteria1 can use
            'Always xlFilterValues as there will always be more than 2 options
            'Also the options are taken from the worksheet live so won't change between times so specifying them precisely as strings is ok
            Range("F").AutoFilter Field:=1, Criteria1:=Application.Transpose(Rng.Value), _
            Operator:=xlFilterValues
        Else
            'Just filter the range but leave all options available
            Range("F").AutoFilter Field:=1
        End If
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

使用第二个计数器来计算应包含在条件中的成功条目,并将它们写入另一个工作表中的范围。 然后将范围设置为新工作表中的新(连续)范围。

现在就像魅力一样。我只花了一整天时间找到适用于Criteria的语法,并认为你只能使用xlOr最多2个标准,否则它是xlfiltervalues ......

最终的工作代码是通用的,因为我可以尽可能地提供帮助:

List<WebElement> keyElements = driver.findElements(By.xpath("//div[@class='col-xs-12 check-box-container']/a/div"));
List<String> keys = new ArrayList<String>();
for(WebElement key:keyElements)
    keys.add(key.getAttribute("id"));

List<WebElement> valueElements = driver.findElements(By.xpath("//div[@class='col-xs-12 check-box-container']/a/div/div"));
List<String> values = new ArrayList<String>();
for(WebElement value:valueElements)
    values.add(value.getAttribute("innerHTML"));

for(int i=0; i<keys.size();i++)
    System.out.println(keys.get(i) + " : " + values.get(i));