VBA - 如果列标题匹配,则在多个表格中应用过滤器

时间:2018-05-09 08:25:43

标签: excel vba excel-vba

我有一个宏来重命名某些标题,删除某些列并在特定的表格上插入一行。

我需要在工作表上应用过滤器 IF 在工作表上找到标题。

with wkbk1语句有2个for loops,我刚刚从第一个循环中复制了第二个循环,并尝试应用过滤器。

以下是代码:

Sub ManipulateSheets()

    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
    Dim a As Long, w As Long
    Dim keepCols As Variant
    Dim wkbk1 As Workbook

    Set wkbk1 = Workbooks("testWorkbook.xlsm")

    'Set sheets to be used in each workbook
    Set ws2 = wkbk1.Sheets("mySheet")
    Set ws3 = wkbk1.Sheets("hisSheet")
    Set ws4 = wkbk1.Sheets("herSheet")

    keepCols = Array("Employee Number", "Status")
    filterCols = Array("Status")

    wkbk1.Activate

    ws2.Activate

    Range("A1").EntireRow.Insert

    Range("A1").Value = "Employee Number"

    ws3.Activate

    Range("A1").EntireRow.Insert

    Range("A1").Value = "Employee Number"

    ws4.Activate

    Range("A1").EntireRow.Insert

    Range("A1").Value = "Employee Number"

    For Each ws1 In wkbk1.Sheets

        ws1.Cells(1, 1).EntireRow.Replace What:="USERID", Replacement:="Employee Number", Lookat:=xlWhole
        ws1.Cells(1, 1).EntireRow.Replace What:="STATUS", Replacement:="Status", Lookat:=xlWhole
        ws1.Cells(1, 1).EntireRow.Replace What:="USER_ID", Replacement:="Employee Number", Lookat:=xlWhole
        ws1.Cells(1, 1).EntireRow.Replace What:="USER-ID", Replacement:="Employee Number", Lookat:=xlWhole
        ws1.Cells(1, 1).EntireRow.Replace What:="USER_STATUS", Replacement:="Status", Lookat:=xlWhole
        ws1.Cells(1, 1).EntireRow.Replace What:="HR_STATUS", Replacement:="Status", Lookat:=xlWhole

    Next ws1

    With wkbk1

        For w = 1 To .Worksheets.count

            With Worksheets(w)

                For a = .Columns.count To 1 Step -1

                    If UBound(Filter(keepCols, .Cells(1, a), True, vbTextCompare)) < 0 Then _
                            .Columns(a).EntireColumn.Delete

                Next a

            End With

        Next w

        For w = 1 To .Worksheets.count

            With Worksheets(w)

                For a = .Columns.count To 1 Step -1

                    If UBound(Filter(filterCols, .Cells(1, a), True, vbTextCompare)) < 0 Then _
                            .AutoFilter Field:=1, Criteria1:=("INACTIVE"), Operator:=xlFilterValues

                Next a

            End With

        Next w

    End With

End Sub

目前,当我运行此代码时,一切都运行良好,直到这一行:

.AutoFilter Field:=1, Criteria1:=("INACTIVE"), Operator:=xlFilterValues

我收到错误:

  

未找到命名参数

1 个答案:

答案 0 :(得分:2)

@EitelDagnin你首先应该使用。查看标题是否存在,然后使用AutoFilter,如:

Set StatusFound = Worksheets(w).Rows(1).Find(What:="Status", LookAt:=xlWhole)
'Above search for the Word "Status" on the first row of the Sheet

If NOT StatusFound is Nothing then
'''''If Status is found then
'''''Apply your filter

    For a = .Columns.count To 1 Step -1
        If UBound(Filter(filterCols, Worksheets(w).Cells(1, a), True, vbTextCompare)) < 0 Then _
            Worksheets(w).UsedRange.AutoFilter Field:=StatusFound.Column, Criteria1:=("INACTIVE"), Operator:=xlFilterValues
        End If
    Next a
End if