如何将条件从字符串数组转移到自动过滤器。 VBA Excel

时间:2015-03-19 13:35:40

标签: excel-vba vba excel

我试过很多方面,但没有一个工作,我想要做的是将数据从字符串数组传输到自动过滤器标准。我的部分代码:

crit(21) = """audi"", ""mercedes"""

    Cells.Find(What:="Film", After:=ActiveCell,  LookIn:=xlFormulas,       LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
    ActiveCell.Select
    Set zasieg = Range(ActiveCell, ActiveCell.Offset(0, 15))
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$P$200000").AutoFilter Field:=14, Criteria1:=Array(crit(21)), Operator:=xlFilterValues

这部分代码在循环中起作用,而一些暴击(i)有5个元素。

3 个答案:

答案 0 :(得分:5)

字符串中创建数组并使用数组

Sub dural()
    Dim MyString As String, r As Range
    MyString = "Larry,Moe,Curly,Shepp"
    ary = Split(MyString, ",")
    Set r = Range("A1:A14")
    With r
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:=(ary), Operator:=xlFilterValues
    End With
End Sub

修改#1:

以下是替代代码(你应该>

Sub DontUseThisCode()
    Dim MyString As String, r As Range
    Dim ary(0 To 3) As String

    ary(0) = "Larry"
    ary(1) = "Moe"
    ary(2) = "Curly"
    ary(3) = "Shepp"
    Set r = Range("A1:A14")
    With r
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:=(ary), Operator:=xlFilterValues
    End With

End Sub

答案 1 :(得分:2)

示例显示如何在没有循环的情况下从范围(将范围转换为数组)构建过滤器,并使用该数组设置过滤器。此示例使用两个工作表(Sheet1,Sheet2) - 其中Sheet1是要过滤的主要数据集。 Sheet2具有过滤器值。

Public Sub xFilter()
   Dim rng As Range
   Dim tmp As String
   Dim val()       
   Dim addr As String
   Dim intStart As Long
   Dim intEnd As Long
   Dim intCol As Integer
   Worksheets("Sheet2").Select ' A worksheet with the filter (possible) values.
   Range("$A$2").Select
   Range(Selection, Selection.End(xlDown)).Select
   Set rng = Selection ' get address
   intStart = rng.Row
   intEnd = intStart + (rng.Count - 1)
   intCol = rng.Column ' next column
   Set rng = Range(Cells(intStart, (intCol + 1)), Cells(intEnd, (intCol + 1)))
   ' convert to text
   rng.FormulaR1C1 = "=Text(rc[-1],0)"
   ' convert to array of text
   val = Application.Transpose(rng)
   rng.FormulaR1C1 = "" ' remove formula
   Set rng = Nothing
   Worksheets("Sheet1").Select
   Range("$A$1").Select
   Range(Selection, Selection.End(xlDown)).Select
   Set rng = Selection
   intStart = rng.Row
   intEnd = intStart + (rng.Count - 1)
   intCol = rng.Column ' next column
   Range(Cells(intStart, 1), Cells(intEnd, 2)).AutoFilter
   Range(Cells(intStart, 1), Cells(intEnd, 2)).AutoFilter _
        Field:=1, Criteria1:=val, Operator:=xlFilterValues
End Sub

答案 2 :(得分:0)

似乎这已在不久前得到解答,但我想问一个其他问题。您可以使用一些用户输入将其变成通用宏吗?

Public Sub xFilter()
   Dim rng As Range
   Dim tmp As String
   Dim val()
   Dim addr As String
   Dim intStart As Long
   Dim intEnd As Long
   Dim intCol As Integer
   Dim FilterLocation As Range
   Dim FilterValues As Range

   Set FilterLocation = Application.InputBox("Select Filter Location", "Obtain Range Object", Type:=8)
   Set FilterValues = Application.InputBox("Selection: Filter Criteria", "Obtain Range Object", Type:=8)


   Worksheets(FilterValues.Worksheet.Name).Select ' A worksheet with the filter (possible) values.
   FilterValues.Select
   Range(Selection, Selection.End(xlDown)).Select
   Set rng = Selection ' get address
   intStart = rng.Row
   intEnd = intStart + (rng.Count - 1)
   intCol = rng.Column ' next column
   Set rng = Range(Cells(intStart, (intCol + 1)), Cells(intEnd, (intCol + 1)))
   ' convert to text
   rng.FormulaR1C1 = "=Text(rc[-1],0)"
   ' convert to array of text
   val = Application.Transpose(rng)
   rng.FormulaR1C1 = "" ' remove formula
   Set rng = Nothing
   Worksheets(FilterLocation.Worksheet.Name).Select
   FilterLocation.Select
   Range(Selection, Selection.End(xlDown)).Select
   Set rng = Selection
   intStart = rng.Row
   intEnd = intStart + (rng.Count - 1)
   'intCol = rng.Column ' next column
   'Range(Cells(intStart, 1), Cells(intEnd, 2)).AutoFilter
   Range(Cells(intStart, 1), Cells(intEnd, 2)).AutoFilter _
        Field:=1, Criteria1:=val, Operator:=xlFilterValues
End Sub

唯一的问题是,当我用它代替最后一行时,会出现错误:

Range(Cells(intStart, 1), Cells(intEnd, 2)).AutoFilter _
    Field:=FilterValues.Column, Criteria1:=val, Operator:=xlFilterValues