错误424:要求对象_将表设置为范围

时间:2019-09-19 11:46:12

标签: excel vba tablename

以下代码用于表中选定行的条件格式。
我试图用表名称引用替换与表相关的范围,以允许在添加新数据时自动更改范围。 但是当我尝试这样做时,它显示以下消息:

enter image description here

代码中突出显示的行是带有红色的行,如下所示。
任何帮助将不胜感激。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("Table1")   
[COLOR="#FF0000"]If Not Intersect(Target, tbl.DataBodyRange.Select) Is Nothing Then  [/COLOR] 
        Dim Cell As Range
        Range("A4").NumberFormat = "@"
        If Target.Count > 1 And Target.Count < 50 Then
        StopCode
        For Each Cell In Selection
        Range("A4").Value = Range("A4").Value & "(" & Cell.Row - 21 & ")"
        Next Cell
        ResetCode
        Else
        Range("A4").Value = "(" & Target.Row - 21 & ")"
        End If
Else
Range("A4").ClearContents
End If
End Sub

1 个答案:

答案 0 :(得分:1)

请尝试以下操作,而不使用Select ...

If Not Intersect(Target, tbl.DataBodyRange) Is Nothing Then
相关问题