如何在VBA中使用自动筛选方法?

时间:2018-07-31 10:35:42

标签: vba

我正在尝试创建一个宏,其中将根据名称对列进行过滤。我创建了一个变量(Cste),用于存储用逗号分隔的名称。但是,当我将其与其他组件连接起来时:

ActiveSheet.Range("$A$1:K" & lrow).AutoFilter Field:=3, Criteria1:=Array(Cste & "="), Operator:=xlFilterValues

所有内容均未选中。

如何通过仅选择存储在Cste中的名称来解决它?

谢谢。

这是完整的代码:

Sub listeDoublonsPlage()
    Dim Plage As Range
    Dim Tableau() As Variant
    Dim Resultat() As String
    Dim i As Integer, j As Integer, m As Integer
    Dim Un As Collection
    Dim Doublons As String
    Dim tablNomtrouves(10000) As String
    Set Un = New Collection


    Sheets("En cours").Select
    lrow = Range("C65536").End(xlUp).Row
    Set Plage = Range("C1:C" & lrow)


    Tableau = Range("C1:C5000").Value

    On Error Resume Next
    'boucle sur la plage à tester
    For i = 1 To Plage.Count

        ReDim Preserve Resultat(2, m + 1)

        'Utilise une collection pour rechercher les doublons
        '(les collections n'acceptent que des données uniques)
        Un.Add Tableau(i, 1), CStr(Tableau(i, 1))

        'S'il y a une erreur (donc présence d'un doublon)
        If Err <> 0 Then


            For j = 0 To m + 1
                'Si oui, on  incrémente le compteur
                If Resultat(1, j) = Tableau(i, 1) Then
                    Resultat(2, j) = Resultat(2, j) + 1
                    Err.Clear
                    Exit For
                End If
            Next j

                'Si non, on ajoute le doublon dans le tableau
                If Err <> 0 Then
                    Resultat(1, m + 1) = Tableau(i, 1)
                    Resultat(2, m + 1) = 1
                    m = m + 1
                    tablNomtrouves(j) = Tableau(i, 1)
                    Err.Clear

                End If
        End If
    Next i

    '----- Affiche la liste et le nombre de doublons --------
    Dim Cste As String
    Index = 1
    tablSirhus = Array("Olga tintin")
    For j = 0 To UBound(tablSirhus)

        Doublons = Doublons & Resultat(1, j) & " --> " & _
                    Resultat(2, j) & vbCrLf
        'MsgBox ("" & tablNomtrouves(j))

    For y = 0 To UBound(tablNomtrouves)
        payday = allIn(LCase(tablSirhus(j)), LCase(tablNomtrouves(y)))

        If payday = True And Len(Trim(tablNomtrouves(y))) <> 0 Then

            MsgBox ("" & tablNomtrouves(y))
            Cste = Cste & """"
            Cste = Cste & " " & tablNomtrouves(y) & ""
            Cste = Cste & """" & ","
            Index = Index + 1
        End If



    Next y
    Next j

    For i = 0 To Index


    Next i

    Set Un = Nothing

    Cste = Left(Cste, Len(Cste) - 0)
    MsgBox Cste


    Sheets("En cours - MER - Evo").Select
    ActiveSheet.Range("$A$1:K" & lrow).AutoFilter Field:=3, Criteria1:=Array(Cste & "="), Operator:=xlFilterValues
End Sub

1 个答案:

答案 0 :(得分:0)

Array(Cste & "=")实际上返回一个由逗号分隔的字符串组成的单元素数组,并在字符串后附加=,而不是由多个元素组成的数组,每个元素都是一个名称。因此,您需要使用Split函数将逗号分隔的字符串转换为数组。

首先,首先在For / Next循环之前为Cste分配一个空字符串(“”)...

Cste = ""

这确保我们以空字符串开头。然后替换...

Cste = Cste & """"
Cste = Cste & " " & tablNomtrouves(y) & ""
Cste = Cste & """" & ",

使用

Cste = Cste & "," & tablNomtrouves(y)

然后,如果要包含空格...

Cste = Cste & ",="

最后,要从字符串中删除第一个逗号...

Cste = Mid(Cste, 2)

现在Cste应该被分配一个字符串,例如...

quentin coldwater,alice quinn,=

现在您可以使用Split函数返回一个数组...

Criteria1:=Split(Cste, ",")