excel - 检查特定工作表是否是事件工作表

标签 excel vba

如何检查特定工作表是否是事件工作表?

我希望将特定功能用于名为数据的工作表。

我可以使用以下代码检查数据表是否存在

Dim ws As Worksheet
Set ws = Wb.Sheets("Data")
If ws Is Nothing Then

Else

但是如何检查数据表是否是事件表? 有没有类似的事情

If ws Is Activesheet Then

更新:

我在插件的类模块之一中添加了以下代码。

我想做的是从此插件管理其他 Excel 工作表。如果事件工作表的名称为“Data”,我想调用过程 paste_cells

Public WithEvents App As Application

Private Sub App_WorkbookActivate(ByVal Wb As Workbook)
MsgBox "Activate"

Dim ws As Worksheet
Set ws = Wb.Sheets("Data")

If ws Is ActiveSheet Then  ' if active sheet is having name Data
App.OnKey "^v", Procedure:="Paste_cell" 'paste cell is procedure i want to add when active sheet is Data
Else
App.OnKey "^v"
End If

End Sub

最佳答案

您还可以检查对象(我们永远不知道用户是否打开了工作表同名的工作簿):

Sub test()
  On Error Resume Next
  If ActiveWorkbook.Worksheets("Data") Is ActiveSheet Then MsgBox ("ok")
  On Error GoTo 0
End Sub

参见MSDN

感谢 brettdj 关于错误处理的提醒。

[编辑]在您的代码中:

Public WithEvents App As Application

Private Sub App_WorkbookActivate(ByVal Wb As Workbook)
MsgBox "Activate"

Dim ws As Worksheet
On Error Resume Next
Set ws = Wb.Sheets("Data")
On Error GoTo 0

If Not ws Is Nothing and ws Is ActiveSheet Then  ' if active sheet is having name Data
  App.OnKey "^v", Procedure:="Paste_cell" 'paste cell is procedure i want to add when active sheet is Data
Else
  App.OnKey "^v"
End If
End Sub

关于excel - 检查特定工作表是否是事件工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7514868/

相关文章:

excel - 如何检查VBA公式是否正确

c# - 不带参数的 Excel DNA 函数在 Excel 函数向导中仍显示一个参数

excel - 相互测试变体

excel - VBA,修剪路径的一部分

excel - 试图在单元格中找到精确字符串的下一个实例

excel - 范围类的 CopyPicture 方法失败 - 有时

vba - 运行时错误 91 : Object variable or With block variable not set

vba - 使用 VBA,是否有更有效的方法来遍历 Excel 中的行,然后在该循​​环中再循环抛出两次?

excel - VBA根据最后一行值选择特定列

vba - Excel 的 Application.Hwnd 属性可以在 64 位 VBA 中使用吗?