excel VBA中有ListObject事件吗?

a0zr77ik  于 2023-01-14  发布在  其他
关注(0)|答案(3)|浏览(206)

我可以从ListObject的.NET API(VSTO)中看到,确实存在该对象的事件。VSTO Docs
然而,我不是在.NET中编码,我只是在做普通的Excel VBA,并希望利用ListObject引发的事件。
The official Microsoft Documentation on ListOjbect不显示任何事件,但我希望也许有一个“非官方”的方式可以实现这一点?

nzkunb0c

nzkunb0c1#

没有直接的Events,但有一些变通方法。
例如,您可以检查用户是否尝试单击ListObject下的内部或一行

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

   Dim tbl As ListObject: Set tbl = ListObjects("Table1")

   If Not Intersect(Target, tbl.Range.Offset(1, 0)) Then
        Exit Sub 'clicked elsewhere, exit
   Else
        'tried to access table do something <code here>
   End If

End Sub
6vl6ewon

6vl6ewon2#

我不知道Listobjects的任何特定事件,但您可以使用工作表的事件轻松地重现该行为。
如果您希望在单击一个单元格时触发事件:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim mytbl As ListObject
Set tbl = thisworkbook.sheets("whatever sheet").ListObjects("Table1")

dim overlap as range
set overlap = Intersect(Target, mytbl.databodyrange)
If Not overlap in nothing Then
   'your selection is totally or partially inside the table
    if overlap.count=1
    ' you selected only one cell
    ' do something
    ' If you want to access the cell selected
    ' use target.range
Else
    msg box('you did not make a proper selection of one cell inside the listobject')
End If
End if

End Sub

如果希望在更改列表对象的单元格值时触发事件:你一次只能改变一个单元格。所以没有必要检查单元格的数量。它总是一个。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim mytbl As ListObject
Set tbl = thisworkbook.sheets("whateversheet").ListObjects("Table1")
' the list object table is in sheet 'whateversheet"
dim overlap as range
set overlap = Intersect(Target, mytbl.databodyrange)
If Not overlap is nothing Then
    ' your selection is inside the table
    ' code is here when you change the value of a cell of the table.
    ' do some stuff
    ' if you want to add the introduced value: 
    newvalue=target.value
Else
    ' You might inform the user that the change took place outside the listobject
End If

End Sub

还有其他事件你可以编程。双击等。
基本上,它总是关于寻找事件是否由属于列表对象的工作表单元格触发(因此是范围的交集)。2如果是,则触发相应的代码。

t3irkdon

t3irkdon3#

我制作了一个软件包(alpha W.I.P,我可能会更改API):
https://github.com/Greedquest/ListObject-WithEvents
从README文档中,将其粘贴到包含表的工作表代码中(还有其他方法可以连接表,但只有工作表、工作簿和类模块可以声明WithEvents

Private WithEvents fooTableEvents As TableWatcher

Sub StartListening() 'press F5 to start listening. You could call this from Workbook_Open
   Set myTable = TableWatcher.Create(Me.ListObjects("foo"))
End Sub

Private Sub fooTableEvents_RowAppended(ByVal where As ListRow)
  Debug.Print "New Row added to table Foo -"; where.DataBodyRange.Address
End Sub

当前暴露的事件列表:

Public Event RowInserted(ByVal where As ListRow)
Public Event RowDeleted(ByVal prevLocation As Range)
Public Event RowAppended(ByVal where As ListRow)
Public Event ColumnInserted(ByVal where As ListColumn)
Public Event ColumnDeleted(ByVal prevLocation As Range)
Public Event ColumnAppended(ByVal where As ListColumn)
Public Event ColumnNameChanged(ByVal which As Range)
Public Event DataValueChanged(ByVal where As Range)
  • 资料来源 *

相关问题