K.C*_*mes 6 excel vba excel-vba
我试图通过VBA在Excel 2013中确定ActiveCell是不是只在任何表中,而是在特定的表中.
下面是代码,但只检测ActiveCell在任何表中.注释掉的行是我正在寻找的,但显然它不起作用.
...
Set rng = Intersect(.EntireRow, ActiveCell.ListObject.DataBodyRange)
'Set rng = Intersect(.EntireRow, ActiveCell.ListObjects("myTable").DataBodyRange)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "Please select the cell of a row within the consensus input table.", vbCritical, "Delete Evaluator"
Else
...
有关正确语法的任何建议吗?
谢谢!
要测试ActiveCell是否在Table1的主体中:
Sub qwerty()
If Intersect(ActiveCell, ActiveSheet.ListObjects("Table1").DataBodyRange) Is Nothing Then
MsgBox "activecell not in Table1"
Else
MsgBox "activecell in Table1"
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
一般来说,我们对在表的 DataBodyRange 内执行的工作感兴趣,Excel 为我们提供了表该区域的快捷方式。对于名为“myTable”的表,您可以使用 直接在代码中访问 DataBodyRange [myTable]。
因此,对于 ActiveCell 的包容性表位置测试,可以进行如下测试:
If Not Intersect(ActiveCell, [myTable]) Is Nothing Then
Run Code Online (Sandbox Code Playgroud)
更通用的解决方案,适用于其他表
Sub Demo()
Dim r As Range
Dim lo As ListObject
Set r = ActiveCell
Set lo = r.ListObject
If Not lo Is Nothing Then
Select Case lo.Name
Case "Table1"
If r.Row = lo.Range.Row Then
MsgBox "In Table1 Header"
Else
MsgBox "In Table1 Body"
End If
Case "SomeOtherTable"
'...
End Select
Else
MsgBox "Not in any table"
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7549 次 |
| 最近记录: |