【问题标题】:Filter with VBA in excel在 Excel 中使用 VBA 过滤
【发布时间】:2021-12-19 12:27:30
【问题描述】:

我正在尝试过滤我的电影列表。不幸的是,它还没有那么好。我找到了一种非常快速的方法,但是这个方法缺少一些选项。 如果我将整列读入一个数组并搜索单个单词,超过 2000 部电影需要相对较长的时间。 我想念的: 在 A 列中,我只能按第一个词过滤。所以它只从标题的开头开始。例如,“F”查找所有“Film*”标题。

在 B 列和 C 列中,我希望能够对“从到”进行排序。所以所有 2012 年之后的电影,例如所有评分都高于 7 的电影。

在 G 列和 H 列中,我又遇到了只能从前面排序的问题。所以如果类型动作排在第二位,我就找不到了。另外我想找到 2 种类型,例如:“* 犯罪 * 动作 *”

效果很好的是我现在已经可以合并了。

Excel 表:

https://mega.nz/file/RsUXRKgD#4ba95fkQOYiteWCH8WST8AuSKZi0k6YCtkuJkOK8tQc

我的代码:

'filter in row2
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long
    
    If Not Application.Intersect(Range("A2:G2"), Range(Target.Address)) Is Nothing Then
        If Cells(2, 1).Value = "" And Cells(2, 2).Value = "" And Cells(2, 3).Value = "" And Cells(2, 4).Value = "" And Cells(2, 5).Value = "" And Cells(2, 6).Value = "" And Cells(2, 7).Value = "" Then
            On Error Resume Next
            ActiveSheet.ShowAllData
            ActiveSheet.Rows.Hidden = False
        Else
            LR = UsedRange.Rows.Count  'includes hidden rows
            Range("A1:G" & LR).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("A1:G3")
        End If
    End If

End Sub 



Private Sub ToggleButton1_Click()
Dim Reihe As String
Reihe = "A"
If ToggleButton1.Value = False Then
Call orderXA(Reihe)
Else
Call orderXD(Reihe)
End If

End Sub
Private Sub ToggleButton2_Click()
Dim Reihe As String
Reihe = "B"
If ToggleButton2.Value = False Then
Call orderXA(Reihe)
Else
Call orderXD(Reihe)
End If
End Sub

Private Sub ToggleButton3_Click()
Dim Reihe As String
Reihe = "C"
If ToggleButton3.Value = False Then
Call orderXA(Reihe)
Else
Call orderXD(Reihe)
End If
End Sub

Private Sub ToggleButton4_Click()
Dim Reihe As String
Reihe = "D"
If ToggleButton4.Value = False Then
Call orderXA(Reihe)
Else
Call orderXD(Reihe)
End If
End Sub

Private Sub ToggleButton5_Click()
Dim Reihe As String
Reihe = "E"
If ToggleButton5.Value = False Then
Call orderXA(Reihe)
Else
Call orderXD(Reihe)
End If
End Sub

Private Sub ToggleButton6_Click()
Dim Reihe As String
Reihe = "F"
If ToggleButton6.Value = False Then
Call orderXA(Reihe)
Else
Call orderXD(Reihe)
End If
End Sub

Private Sub ToggleButton7_Click()
Dim Reihe As String
Reihe = "G"
If ToggleButton7.Value = False Then
Call orderXA(Reihe)
Else
Call orderXD(Reihe)
End If
End Sub
   

Sub orderXA(Reihe As String)
LR = UsedRange.Rows.Count  'zählt uf versteckte mit
Range("A3:H" & LR).Sort Key1:=Range(Reihe & "4"), order1:=xlAscending, Header:=xlYes

End Sub
Sub orderXD(Reihe As String)
LR = UsedRange.Rows.Count  'zählt uf versteckte mit
Range("A3:H" & LR).Sort Key1:=Range(Reihe & "4"), order1:=xlDescending, Header:=xlYes

End Sub

【问题讨论】:

    标签: excel vba filter


    【解决方案1】:

    为什么这么复杂?只需穿上自动过滤器。你甚至不需要 VBA。

    对不起,德国的截图。

    【讨论】:

    • Danke, da sieht man den Wald vor lauter Bäumen nicht。 So is es natürlich viel einfacher.
    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多