基于ActiveCell值(VBA)筛选表的dynamic超链接

我正在创build一个dynamic超链接来过滤另一个工作表上的表(Sheet15)。

我的目标是让用户能够在Sheet3上select一个单元格,并将该单元格的VALUE作为另一个表单上的filter。

这是我的代码到目前为止:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) If Target.Type = msoHyperlinkRange And Target.Range.Address = "$S$15" Then Application.ScreenUpdating = False Sheet15.Visible = True Sheet15.ListObjects("Table17").Range.AutoFilter Field:=19, Criteria1:=ActiveCell.Value Sheet15.Activate Application.ScreenUpdating = True End If End Sub 

但是,当我点击超级链接时,表格根本就不被过滤,所以我必须做一些错误的事情。

任何人都可以协助


UPDATE

这是更新的代码。

单元格S17现在是我想过滤表的值:

 Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) If Target.Type = msoHyperlinkRange And Target.Range.Address = "$S$15" Then Application.ScreenUpdating = False Sheet15.Visible = True Sheet15.ListObjects("Table17").Range.AutoFilter Field:=19, Criteria1:=Sheet3.Range("S17").Value Sheet15.Activate Application.ScreenUpdating = True End If End Sub 

但问题依然存在。 当我点击他们的超链接,我会被带到另一张表,但表格根本不被过滤。

坚持原来的计划,并假设列“A”是一个城市名称,在工作表代码窗格中放置以下内容

 Option Explicit Dim lastCell As Range '<--| declare a module scoped range variable to store the last cell selected by the user, if "valid" Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Address = "$S$15" Then Exit Sub '<-- do nothing if user selected cell with hyperlink Set lastCell = Intersect(Target, Columns("A")) '<-- change "Columns("A") to a named range with your cities End Sub Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) If lastCell Is Nothing Then Exit Sub '<--| no action if lastCell has not been properly set by 'Worksheet_SelectionChange()' If Target.Type = msoHyperlinkRange And Target.Range.Address = "$S$15" Then Application.ScreenUpdating = False Sheet15.Visible = True Sheet15.ListObjects("Table17").Range.AutoFilter Field:=19, Criteria1:=lastCell.Value '<--| set the criteria as 'lastCell' value Sheet15.Activate Application.ScreenUpdating = True End If End Sub 

根据注释,将Worksheet_SelectionChange() Columns("A")引用更改为实际的城市名称(也许是命名的范围

注意:除非超链接指向自身,否则ActiveCell.Value将是链接目标 :如果您希望包含链接的单元格的值使用Target.Range.Value

 Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) If Target.Type = msoHyperlinkRange And Target.Range.Address = "$S$15" Then Application.ScreenUpdating = False With Sheet15 .Visible = True .ListObjects("Table17").Range.AutoFilter Field:=19, _ Criteria1:=Target.Range.Value .Activate End With Application.ScreenUpdating = True End If End Sub