【问题标题】:Clicking a hyperlink in Excel to set autofilter on a different sheet单击 Excel 中的超链接以在不同的工作表上设置自动过滤器
【发布时间】:2016-02-05 19:16:26
【问题描述】:

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

我正在尝试做的是让用户单击第一张纸上公司的单元格,然后将用户带到下一张纸,其中自动过滤器已经填充了所选公司。这样,用户只能直接访问所选公司的董事会成员。

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

【问题讨论】:

  • 是的。这里需要 VBA。查看Worksheet_BeforeDoubleClick 事件,然后告诉用户双击单元格将执行该操作。如果您需要更多具体帮助,您需要发布您正在构建的代码以及它不工作的地方。

标签: vba excel


【解决方案1】:

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

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

【讨论】:

  • 谢谢乔纳森,我知道它是如何工作的。我做了一些改动,在公司名称列右侧的列中包含一个链接。我想 Criteria1: 项目现在需要引用左侧的活动单元格。
  • 不客气。是的,它需要引用具有您要传递的名称的单元格。如果要选择单击左侧的单元格,可以使用 activecell.offset(0,-1).value
  • 最后一个问题。如果我想将点击操作限制在公司工作表中的特定表格列,我该怎么做?
  • 我在 sub 中添加了一个 if 语句来检查列号。只需将 1 更改为您需要的列,它应该适合您。
【解决方案2】:

您需要打开 Visual Basic 编辑器,右键单击公司工作表, 查看代码,粘贴到:

Option Explicit

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim CompanyName As String

        If Selection.Count = 1 Then
            If Not Intersect(Target, Range("C1").EntireColumn) Is Nothing Then
                'This code is triggered when any
                'ONE cell in column C is selected
                'Simply change "C1" to "B1" etc etc


                'This MsgBox returns the selected cell
                MsgBox Target.Address

                'You'll probably need to collect some information
                'in this section. You can then use this to affect
                'the filters on sheet 2.
                'Perhaps like this
                CompanyName = Cells(Target.Row, 1).Value
                MsgBox CompanyName

                'This changes to "Sheet2"
                Sheets("Sheet2").Activate

            End If
        End If
    End Sub

希望对您有所帮助,您可以有所作为

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2016-05-02
    • 2019-06-28
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多