不同工作表上的选择更改

时间:2017-05-29 08:17:42

标签: excel vba excel-vba filtering

您好我正在尝试找到有关选择更改事件的问题的解决方案: 我有一个工作表,其中包含产品列表和相关的所有详细信息。在另一个工作表上,我有与其他信息相同的产品。我想要做的是点击第一张纸上的产品单元格,然后在第二张纸张中过滤。 ON / OFF选项用于在已启用双击宏的文件中启用/禁用此功能。感谢您的研究

 Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim rngF As Range
    Dim rngFS As Range

    Dim lRow As Long
    Dim lCol As Long
    Set rngF = ActiveSheet.AutoFilter.Range
    Set rngFS = ActiveSheet.Range("FilterStatus")

    lCol = rngF.Columns(1).Column - 1
    lRow = rngF.Columns(1).Row

    If Target.Count > 1 Then GoTo exitHandler

    If Target.Address = rngFS.Address Then
      If rngFS.Value = "On" Then
        rngFS.Value = "Off"
      Else
        rngFS.Value = "On"
      End If
    End If

    If UCase(rngFS.Value) = "ON" Then
      If Not Intersect(Target, rngF) Is Nothing Then
        If Target.Row > lRow Then
          rngF.AutoFilter Field:=Target.Column - lCol, _
              Criteria1:=Target.Value
        ElseIf Target.Row = lRow Then
          rngF.AutoFilter Field:=Target.Column - lCol
        End If
      End If
    End If

    exitHandler:
      Exit Sub

    End Sub

0 个答案:

没有答案