gpt4 book ai didi

excel - 在 Excel 中反转过滤器选择

转载 作者:行者123 更新时间:2023-12-04 19:45:39 25 4
gpt4 key购买 nike

我在问一个我计划回答的问题,以便我可以以持久的方式记录这个问题。非常高兴其他人提出其他建议/更正。

我经常在 Excel 中遇到问题,我正在使用过滤器,然后想要反转选择,即取消选择所有已选择的项目并选择当前未选择的所有项目。例如,请参阅下面的屏幕截图:

除了单击列表之外,没有简单的方法可以做到这一点(我知道!),这既费力又容易出错。我们如何在 Excel 中自动实现此功能?

前:

Initial selection

后:

Inverted selection

最佳答案

我编写了一些扩展 Excel 并提供此功能的 VBA。它从过滤器子菜单中添加了一个新的上下文菜单(右键菜单)选项(见截图)。
您需要调用AddToCellMenu子程序使菜单项出现。如果您想为所有 Excel session 永久保存,您需要将此代码放入您正在运行的个人工作簿或加载项中,然后调用 AddToCellMenuWorkbook_Open事件,或类似的东西。
enter image description here
无论如何,这是代码:

Option Explicit

Public Sub AddToCellMenu(dummy As Byte)

Dim FilterMenu As CommandBarControl

' Delete the controls first to avoid duplicates
Call DeleteFromCellMenu

' Set ContextMenu to the Cell context menu
' 31402 is the filter sub-menu of the cell context menu
Set FilterMenu = Application.CommandBars("Cell").FindControl(ID:=31402)

' Add one custom button to the Cell context menu
With FilterMenu.Controls.Add(Type:=msoControlButton, before:=3)
.OnAction = "'" & ThisWorkbook.name & "'!" & "InvertFilter"
.FaceId = 1807
.Caption = "Invert Filter Selection"
.Tag = "My_Cell_Control_Tag"
End With

End Sub

Private Sub DeleteFromCellMenu()

Dim FilterMenu As CommandBarControl
Dim ctrl As CommandBarControl

' Set ContextMenu to the Cell context menu
' 31402 is the filter sub-menu of the cell context menu
Set FilterMenu = Application.CommandBars("Cell").FindControl(ID:=31402)

' Delete the custom controls with the Tag : My_Cell_Control_Tag
For Each ctrl In FilterMenu.Controls
If ctrl.Tag = "My_Cell_Control_Tag" Then
ctrl.Delete
End If
Next ctrl

End Sub

Public Sub InvertFilter()

Dim cell As Range
Dim af As AutoFilter
Dim f As Filter
Dim i As Integer

Dim arrCur As Variant
Dim arrNew As Variant
Dim rngCol As Range
Dim c As Range
Dim txt As String
Dim bBlank As Boolean

' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' INITAL CHECKS
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Set cell = ActiveCell

Set af = cell.parent.AutoFilter

If af Is Nothing Then
MsgBox "No filters on current sheet"
Exit Sub
End If

If Application.Intersect(cell, af.Range) Is Nothing Then
MsgBox "Current cell not part of filter range"
Exit Sub
End If

i = cell.Column - af.Range.cells(1, 1).Column + 1
Set f = af.Filters(i)

If f.On = False Then
MsgBox "Current column not being filtered. Nothing to invert"
Exit Sub
End If

' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' GET CURRENT FILTER DATA
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

' Single value criteria
If f.Operator = 0 Then
If f.Criteria1 = "<>" Then ArrayAdd arrNew, "="
If f.Criteria1 = "=" Then ArrayAdd arrNew, "<>"
ArrayAdd arrCur, f.Criteria1
' Pair of values used as criteria
ElseIf f.Operator = xlOr Then
ArrayAdd arrCur, f.Criteria1
ArrayAdd arrCur, f.Criteria2
' Multi list criteria
ElseIf f.Operator = xlFilterValues Then
arrCur = f.Criteria1
Else
MsgBox "Current filter is not selecting values. Cannot process inversion"
Exit Sub
End If

' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' COMPUTE INVERTED FILTER DATA
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

' Only process if new list is empty
' Being non-empty implies we're just toggling blank state and new list is already determined for that
If IsEmpty(arrNew) Then

' Get column of data, ignoring header row
Set rngCol = af.Range.Resize(af.Range.Rows.Count - 1, 1).Offset(1, i - 1)
bBlank = False

For Each c In rngCol

' Ignore blanks for now; they get special processing at the end
If c.Text <> "" Then

' If the cell text is in neither the current filter list ...
txt = "=" & c.Text
If Not ArrayContains(arrCur, txt) Then

' ... nor the new proposed list then add it to the new proposed list
If Not ArrayContains(arrNew, txt) Then ArrayAdd arrNew, txt

End If

Else
' Record that we have blank cells
bBlank = True
End If

Next c

' Process blank options
' If we're not currently selecting for blanks ...
' ... and there are blanks ...
' ... then filter for blanks in new selection
If (Not arrCur(UBound(arrCur)) = "=" And bBlank) Then ArrayAdd arrNew, "="

End If

' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' APPLY NEW FILTER
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Select Case UBound(arrNew)
Case 0:
MsgBox "Didn't find any values to invert"
Exit Sub
Case 1:
af.Range.AutoFilter _
Field:=i, _
Criteria1:=arrNew(1)
Case 2:
af.Range.AutoFilter _
Field:=i, _
Criteria1:=arrNew(1), _
Criteria2:=arrNew(2), _
Operator:=xlOr
Case Else:
af.Range.AutoFilter _
Field:=i, _
Criteria1:=arrNew, _
Operator:=xlFilterValues
End Select

End Sub

Private Sub ArrayAdd(ByRef a As Variant, item As Variant)

Dim i As Integer

If IsEmpty(a) Then
i = 1
ReDim a(1 To i)
Else
i = UBound(a) + 1
ReDim Preserve a(1 To i)
End If

a(i) = item

End Sub

Private Function ArrayContains(a As Variant, item As Variant) As Boolean

Dim i As Integer

If IsEmpty(a) Then
ArrayContains = False
Exit Function
End If

For i = LBound(a) To UBound(a)
If a(i) = item Then
ArrayContains = True
Exit Function
End If
Next i

ArrayContains = False

End Function

' Used to find the menu IDs
Private Sub ListMenuInfo()

Dim row As Integer
Dim Menu As CommandBarControl
Dim MenuItem As CommandBarControl
Dim SubMenuItem As CommandBarControl

row = 1
On Error Resume Next
For Each Menu In CommandBars("cell").Controls
For Each MenuItem In Menu.Controls
For Each SubMenuItem In MenuItem.Controls
cells(row, 1) = Menu.Caption
cells(row, 2) = Menu.ID
cells(row, 3) = MenuItem.Caption
cells(row, 4) = MenuItem.ID
cells(row, 5) = SubMenuItem.Caption
cells(row, 6) = SubMenuItem.ID
row = row + 1
Next SubMenuItem
Next MenuItem
Next Menu

End Sub

关于excel - 在 Excel 中反转过滤器选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48423684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com