excel vba自动筛选器

时间:2019-02-10 23:50:13

标签: excel vba

我有一个标题在第2行开始的数据库,标题在第2行,而信息下面的所有其他标题。我有11列在上面。 第一行将是对自动过滤器进行输入搜索的行,但我只想对某些列进行此操作。

我只希望自动过滤器输入在E,F,H和I列上起作用 所有其他人的输入都无济于事。

但是,当尝试分别设置倍数条件时,自动过滤器不起作用,也不显示任何内容。如果我开始在其他列上写东西,它只会在我在E列上写东西时才起作用

红色背景应该是不可编辑的单元格 绿色的应该是可编辑以进行搜索/过滤的单元格

按钮1是添加更多行的功能,但现在我还没有完成这一部分,试图找出过滤器侧面的火花

这是我的代码:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect
Application.EnableEvents = False

Dim sht As Worksheet
Dim LastRow As Long
Dim DataRange As Range

Set sht = ActiveSheet
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

Set DataRange = Range("A3:J" & LastRow)

If Target.Row = 1 Then
    If Target.Column > 1 Then
        If Target.Column = 5 Then
            Cells(Target.Row, 6).NumberFormat = "0"

            Cells(Target.Row, 9).NumberFormat = "General"
            Cells(Target.Row, 9).Value = "Figueira da Foz"
        End If
    End If
    DataRange.AutoFilter Field:=1, VisibleDropDown:=False
    DataRange.AutoFilter Field:=2, VisibleDropDown:=False
    DataRange.AutoFilter Field:=3, VisibleDropDown:=False
    DataRange.AutoFilter Field:=4, VisibleDropDown:=False
    DataRange.AutoFilter Field:=5, Criteria1:="*" & Cells(Target.Row, 5).Value & "*", VisibleDropDown:=False
    DataRange.AutoFilter Field:=6, VisibleDropDown:=False
    DataRange.AutoFilter Field:=7, Criteria1:="*" & Cells(Target.Row, 7).Value & "*", VisibleDropDown:=False
    DataRange.AutoFilter Field:=8, Criteria1:="*" & Cells(Target.Row, 8).Value & "*", VisibleDropDown:=False
    DataRange.AutoFilter Field:=9, VisibleDropDown:=False
    If Target.Column = 10 Then
        If Target.Value = vbNullString Then
            DataRange.AutoFilter Field:=10, VisibleDropDown:=False
        Else
            DataRange.AutoFilter Field:=10, Criteria1:=Cells(Target.Row, 10).Value, VisibleDropDown:=False
        End If
    End If
End If
If Target.Row = 2 Then
    Range("A" & Target.Row & ":J" & Target.Row).Locked = True
End If
If Target.Row > 2 Then
    If Target.Column = 5 Then
        Cells(Target.Row, 1).NumberFormat = "0"
        Cells(Target.Row, 1).Value = Target.Row - 2

        Cells(Target.Row, 2).NumberFormat = "yyyy-mm-dd"
        Cells(Target.Row, 2).Value = Now

        Cells(Target.Row, 3).NumberFormat = "hh:mm"
        Cells(Target.Row, 3).Value = Now

        Cells(Target.Row, 6).NumberFormat = "0"

        Cells(Target.Row, 9).NumberFormat = "General"
        Cells(Target.Row, 9).Value = "Figueira da Foz"

        Cells(Target.Row, 10).NumberFormat = "General"
        Cells(Target.Row, 10).Value = "Activa"

        Range("A" & Target.Row & ":J" & Target.Row).Interior.ColorIndex = 4
    End If
    If Target.Column = 10 Then
        If Target.Value = "Inactiva" Then
            Cells(Target.Row, 4).NumberFormat = "hh:mm"
            Cells(Target.Row, 4).Value = Now
            Range("A" & Target.Row & ":J" & Target.Row).Interior.ColorIndex = 3
        End If
        If Target.Value = "Activa" Then
            Cells(Target.Row, 4).Value = vbNullString
            Range("A" & Target.Row & ":J" & Target.Row).Interior.ColorIndex = 4
        End If
        If Target.Value = vbNullString Then
            Range("A" & Target.Row & ":J" & Target.Row).Interior.ColorIndex = 2
        End If
    End If
End If

Application.DisplayAlerts = False
ThisWorkbook.Save
Application.EnableEvents = True
ActiveSheet.Protect
End Sub

0 个答案:

没有答案