vba 当我选择一张纸时会发生什么事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19472014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What event happens when I select a sheet?
提问by Jonjon_00
What I am trying to accomplish is fairly simple. When a user selects a sheet, I would like a message box to appear. Meaning: I'm currently viewing Sheet1, I click on the Sheet2 tab and a message pops up before I can do anything. I can't seem to find the event that fires when moving to a different sheet.
我想要完成的事情相当简单。当用户选择工作表时,我希望出现一个消息框。含义:我当前正在查看 Sheet1,我单击了 Sheet2 选项卡,然后在我可以执行任何操作之前弹出一条消息。我似乎无法找到移动到不同工作表时触发的事件。
Events I've tried: Workbook_SheetActivate
and Worksheet_Activate
我尝试过的事件:Workbook_SheetActivate
和Worksheet_Activate
Private Sub Workbook_SheetActivate(ByVal sh As Object)
MsgBox ("Example Message")
End Sub
Or
或者
Private Sub Worksheet_Activate()
MsgBox ("Example Message")
End Sub
I've done some googling and most things are about when cell values change or the cell selection changes.
我已经做了一些谷歌搜索,大多数事情都是关于单元格值更改或单元格选择更改的时间。
回答by Gary's Student
This should work:
这应该有效:
Private Sub Worksheet_Activate()
MsgBox "you never visit...you never call....you never write"
End Sub
However:
然而:
- code must be in the worksheet code area
- macros must be enabled
- events must be enabled
- 代码必须在工作表代码区
- 必须启用宏
- 必须启用事件
回答by Mark Fitzgerald
You could use the following in the "ThisWorkbook" module to fire a message whenever you change sheets within the workbook.
每当您更改工作簿中的工作表时,您都可以在“ThisWorkbook”模块中使用以下内容来触发消息。
Sub Workbook_SheetActivate(ByVal Sh As Object)
MsgBox Sh.Name & " activated!"
End Sub
This will solve the problem without having to add Private Sub Worksheet_Activate()
to every worksheet's code module.
这将解决问题,而无需添加Private Sub Worksheet_Activate()
到每个工作表的代码模块。
回答by jordanhill123
Here is a link to all the worksheet events available in Excel:
以下是 Excel 中所有可用工作表事件的链接:
http://dmcritchie.mvps.org/excel/event.htm
http://dmcritchie.mvps.org/excel/event.htm
Private Sub Worksheet_Activate()
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) -- (additional examples)
Cancel = True 'turn off Edit mode when using “Edit directly in a cell”
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True 'turn off Edit mode when using “Edit directly in a cell”
Private Sub Worksheet_Calculate()
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False 'should be part of Change macro
Application.EnableEvents = True 'should be part of Change macro
Private Sub Worksheet_Deactivate()
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Doesn't Worksheet_Activate
work for you?
不Worksheet_Activate
适合你?
回答by KenSquare
You need to have the event in the worksheet that is being activated - that is, if you put it is sheet 2, it will fire only when that sheet is opened. This worked in sheet 2 of my workbook. Sub worksheet_activate() MsgBox "activated!" End Sub
您需要在正在激活的工作表中设置事件 - 也就是说,如果您将其放在工作表 2 中,则只有在打开该工作表时才会触发。这在我的工作簿的表 2 中起作用。Sub worksheet_activate() MsgBox "已激活!" 结束子