|
- Private Sub auto_open()
-
- With Application.CommandBars("Formatting").Controls.Add(Type:=msoControlButton, temporary:=True)
- .Caption = "自动筛选(&Q)"
- .OnAction = "自动筛选"
- .FaceId = 497
- .TooltipText = "自动筛选当前列与当前单元格相同的内容"
- .Style = msoButtonIconAndCaption
- End With
- Application.OnKey "%{q}", "自动筛选"
- End Sub
- Private Sub auto_close()
- On Error Resume Next
- Application.CommandBars("Formatting").Controls("自动筛选(&Q)").Delete
- End Sub
- Sub 自动筛选()
- arr = Range(ActiveSheet.Range("a1"), ActiveSheet.Cells.SpecialCells(xlLastCell)) '把表中数据赋值给数值
- If IsArray(arr) = False Then MsgBox "当前表格中没有数据,或只有一个数据": Exit Sub
- If UBound(arr, 1) = 1 Then MsgBox "当前表格中只有一行数据不需要筛选": Exit Sub
- If ActiveSheet.FilterMode = True Then Call 取消筛选 Else: Call 执行筛选 '根据当前筛选状态执行对应命令'
- End Sub
- Sub 取消筛选()
- Dim myrng$ '单元格地址
- myrng = Selection.Address '获取当前选定的地址
- Cells.AutoFilter '取消筛选'
- Range(myrng).Select '选择原先的单元格'
- Application.StatusBar = False '恢复状态栏的显示'
- End Sub
- Sub 执行筛选()
- On Error GoTo line
- Dim ic%, rng As Range, myrng$ '列号,单元格'
- Dim kg As Boolean
- Application.ScreenUpdating = False '关闭屏闭
- If Application.Version <> "11.0" Then
- Call 多值筛选
- Else
- If Selection.Rows.Count = 1 Then ''如果选定的单元格的行数等于1
- If Range("A1") = "" Then Range("a1") = " ": kg = True
- For Each rng In Selection '在选定范围中循环'
- ic = rng.Column '获取列号'
- myrng = rng.Value
- If IsDate(myrng) = True Then myrng = Application.WorksheetFunction.Text(myrng, rng.NumberFormat) '单元格值赋给myrng'
- Cells.AutoFilter ic, myrng '在对应列号中筛选等于对应的单元格的值'
- Next
- '--------------------------------------------------
- If kg = True Then Range("a1") = ""
- ActiveWindow.ScrollRow = 1 '窗口跳转到第1行
- If ActiveSheet.FilterMode Then
- With ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
- Application.StatusBar = "筛选结果为" & .Count / .Columns.Count - 1 & "个记录 【如需更多其他方便功能 请联系QQ379069784】"
- End With
- End If
- Else
- MsgBox "2003版的不支持多行数据筛选,只支持单行数据筛选", 48, "SoSo提示您"
- End If
- End If
- Application.ScreenUpdating = True
- line:
- End Sub
- Sub 多值筛选()
- On Error GoTo line
- Dim i%, j%, arr, x1$
- Dim ic%, ir%, q%, xx
- Dim d As Object, dic As Object
- Set d = CreateObject("scripting.dictionary")
- Set dic = CreateObject("scripting.dictionary")
- '--------------------------------------------------
- For Each rng In Selection '在选定范围中循环'
- With rng
- ic = .Column '获取列号'
- If IsDate(.Value) = True Then
- x1 = Application.WorksheetFunction.Text(.Value, rng.NumberFormat)
- Else
- x1 = .Value
- End If
- '--------------------------------------------------
- If x1 = "" Then x1 = "="
- If dic.exists(ic) = True Then
- dic(ic) = dic(ic) & "|" & x1
- Else
- dic(ic) = x1
- End If
- End With
- '--------------------------------------------------
- Cells.AutoFilter Field:=ic, Criteria1:=Split(dic(ic), "|"), Operator:=xlFilterValues
- Next
- line:
- End Sub
复制代码
|
|