单击 Excel 中的超链接以在不同工作表上设置自动筛选

Jas*_*son 3 excel vba

我有一个包含两张表的 Excel 工作簿,基本上是两张表之间的一对多设置。第一张表列出了数百家公司,第二张表列出了这些公司的董事会。第二张表有一个自动过滤器,因此用户可以查看从过滤器中选择的特定公司的董事会成员。

我试图做的是让用户单击第一张工作表上公司的单元格,以便用户进入下一张工作表,其中自动过滤器已经填充了所选公司。这样,用户只能直接访问所选公司的董事会成员。

我想这需要 VBA,并希望有人能为我指明创建此代码来解决此问题的正确方向。非常感谢。

Jon*_*han 5

您可以通过在工作表模块中执行以下操作来完成此操作:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    'Update Table14 to your table name
    'Update Field to column number of the field you are filtering
    'Update Sheet7 to reference the sheet containing your table
    'Change on to the column number where your click should cause this action
    If ActiveCell.Column = 1 Then
    Sheet7.ListObjects("Table14").Range.AutoFilter Field:=1, Criteria1:=ActiveCell.Value
    'Update Sheet7 to reference the sheet containing your table
    Sheet7.Activate
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)