[英]Select cell in worksheet_open, ONLY the first time it is activated
I was looking for a script that selects a cell like ("A5")
to reset the view of the sheet, but only the first time the sheet is activated.我一直在寻找一个脚本,它选择一个像
("A5")
这样的单元格来重置工作表的视图,但只是在第一次激活工作表时。
I have a basic Worksheet_Activate, but this resets me every time I move between sheets and back.我有一个基本的 Worksheet_Activate,但每次我在工作表之间移动和返回时都会重置我。
Thanks in advance!提前致谢!
Private Sub Worksheet_Activate()
Range("A5").Select
End Sub
Assuming you mean, "In a multisheet workbook, whenever I select/activate a certain sheet, I want to control (reset) the view of that sheet by selecting a certain cell, but I only want that to happen the first time I select/activate that sheet, during the time the workbook is open."假设您的意思是,“在多工作表工作簿中,每当我选择/激活某个工作表时,我想通过选择某个单元格来控制(重置)该工作表的视图,但我只希望在我第一次选择/在工作簿打开期间激活该工作表。”
Public variables persist while the workbook is open, maybe this:工作簿打开时,公共变量仍然存在,可能是这样的:
Public isInitialized As Boolean
Private Sub Worksheet_Activate()
If Not isInitialized Then
'put your first time only code here, ie Range("A5").Select
isInitialized = True
End If
End Sub
The Public variable will be false until the first time the designated sheet activates, then will persist True while the workbook is open.在指定工作表第一次激活之前,Public 变量将为 false,然后在工作簿打开时将保持为 True。
Note that the Worksheet_Activate event doesn't fire between open workbooks, in that case, use the Workbook_Activate event in ThisWorkbook.请注意,Worksheet_Activate 事件不会在打开的工作簿之间触发,在这种情况下,请使用 ThisWorkbook 中的 Workbook_Activate 事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.