对象必需的错误 - 双击工作表中的单元格以在另一个中过滤

时间:2018-02-09 13:15:41

标签: excel-vba vba excel

我正在尝试使用以下VBA代码双击Worksheet上的一个单元格,然后在另一个工作表中获取一个已过滤的列,但它给出了一个" Object Required Error"我似乎无法找到:

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

这是来自另一篇文章但似乎有所修改。我正在使用Excel 2013。

谢谢,

Leandro的

1 个答案:

答案 0 :(得分:0)

代码工作正常,但我还是做了一些改动:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column = 1 Then
    Worksheets("SheetName").ListObjects("Table14").Range.AutoFilter Field:=1, Criteria1:=Target.Value
    'amend the SheetName above and below and make sure your table name is correct too
    Worksheets("SheetName").Select
    End If
End Sub

您的错误的原因是您没有Sheet7,您应该将其更改为Worksheets(“SheetName”),或者它可能是您需要修改的表名。