Clicking a hyperlink in Excel to set autofilter on a different sheet

前端 未结 2 1231

I have an Excel workbook with two sheets, basically a one-to-many setup between the two sheets. The first sheet lists several hundred companies and the second sheet lists

相关标签:
2条回答
  • 2020-12-18 13:52

    You can accomplish this by doing something this in the worksheet module:

    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
    
    0 讨论(0)
  • 2020-12-18 14:00

    You'll need to open the visual basic editor, right click the company worksheet, view code, paste this in:

    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
    

    Hope that helps and you can make something of it

    0 讨论(0)
提交回复
热议问题