如何检查特定工作表是否是事件工作表?
我希望将特定功能用于名为数据的工作表。
我可以使用以下代码检查数据表是否存在
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/