简体   繁体   English

1 个工作表中不同范围内的多次双击事件

[英]Multiple Double Click Events on Different Ranges in 1 Worksheet

I'm trying to simulate an interactive "dashboard" in Excel where double clicking specific ranges will Autofilter different parameters on another sheet.我正在尝试在 Excel 中模拟交互式“仪表板”,其中双击特定范围将自动过滤另一张纸上的不同参数。

I'll have a lot of them, probably >10.我会有很多,可能> 10。 Here's generally what I have so far:到目前为止,这通常是我所拥有的:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)

Application.EnableEvents = False
On Error GoTo ErrorRoutine

Dim Goal As String
Goal = "*" & Cells(Target.Row, 7) & "*"

If Not Intersect(Target, Range("L7:L166")) Is Nothing Then

    On Error Resume Next
    Sheets("Details").ShowAllData
    
    Sheets("Details").UsedRange.AutoFilter field:=12, Criteria1:="Open"
    Sheets("Details").UsedRange.AutoFilter field:=1, Criteria1:=Goal
    Sheets("Details").Activate
    
    Cancel = True

ElseIf Not Intersect(Target, Range("M7:M166")) Is Nothing Then
    
    On Error Resume Next
    Sheets("Details").ShowAllData

    Sheets("Details").UsedRange.AutoFilter field:=1, Criteria1:=Goal
    Sheets("Details").Activate
    
    Cancel = True
 
ElseIf etc.etc.

End If

ErrorRoutine:
Application.EnableEvents = True

End Sub

The ElseIf chains just go on. ElseIf 仅链接 go 。 It works but there's a big issue I just can't debug that I think has something to do with how I clear the filters with.ShowAllData between each If/Then.它有效,但有一个大问题我无法调试,我认为这与我如何在每个 If/Then 之间使用.ShowAllData 清除过滤器有关。

For some reason whenever a filter returns 0 values, it won't properly reset to "ShowAllData" when you trigger another double click event that comes after it in the code.由于某种原因,每当过滤器返回 0 值时,当您触发代码中紧随其后的另一个双击事件时,它不会正确重置为“ShowAllData”。 So that event's filter will not return any values even if there definitely are.因此,即使肯定有,该事件的过滤器也不会返回任何值。

Anything weird in my code?我的代码中有什么奇怪的地方吗?

There's no need to turn off events here, since the actions you're taking won't re-trigger the same event code.此处无需关闭事件,因为您正在执行的操作不会重新触发相同的事件代码。

To streamline the code, I'd extract the filter logic to a stand-alone method, and call that from the event handler with the appropriate parameters为了简化代码,我将过滤器逻辑提取到一个独立的方法中,并使用适当的参数从事件处理程序中调用它

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)

    Dim Goal As String, col As String
    
    If Target.Row < 7 Or Target.Row > 166 Then Exit Sub
    
    Goal = "*" & Me.Cells(Target.Row, 7).Value & "*"
    'Target column letter
    col = Replace(Target.EntireColumn.Cells(1).Address(False, False), "1", "")
    
    Select Case col
        Case "L": FilterDetails Array(12, "Open", 1, Goal), Cancel
        Case "M": FilterDetails Array(1, Goal), Cancel
        'add other cases
    End Select
    
End Sub

'Filter Details using one or more criteria passed in `arrSettings`
'  (as pairs of column number/filter value)
Sub FilterDetails(arrSettings, ByRef Cancel As Boolean)
    Dim i As Long
    With ThisWorkbook.Worksheets("Details")
        If .AutoFilterMode Then .AutoFilter.ShowAllData
        For i = LBound(arrSettings) To UBound(arrSettings) - 1 Step 2
            .UsedRange.AutoFilter field:=arrSettings(i), Criteria1:=arrSettings(i + 1)
        Next i
        .Activate
        Cancel = True
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM