简体   繁体   English

worksheet_open 中的 Select 单元格,仅在第一次激活时

[英]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.

相关问题 在活动用户表单的情况下运行Worksheet_Open代码 - Run Worksheet_Open code with a userform active Excel中的“工作表更改”事件仅在我打开工作簿第一次时有效。 之后对单元格所做的任何更改均不起作用 - Worksheet Change event in excel only works the first time i Open the workbook. any changes to cell after do not work 如何从命令行启动 Excel 宏(没有 Worksheet_Open 事件)? - How to Launch an Excel macro from command line (Without Worksheet_Open Event)? 从工作表中选择单元格副本到另一个工作表 - Select cell copy from worksheet to another worksheet 将值单元格工作表复制到另一列中的第一个空单元格,没有公式复制但仅显示 - Copy Value Cell worksheet to anothers first empty cell in a column, no formula copy but only displayed 如何 select 工作表上的特定单元格? - How to select a specific cell on a worksheet? 打开工作簿后,如何仅在第一次激活特定工作表时才运行宏? - How to run a macro only for the first time a particular sheet is activated after opening the workbook? 如何打开以工作表中的单元格命名的工作簿? - How to open a workbook named after a cell in the worksheet? 运行时错误1004试图在工作表中的UserInterface与保护,只有锁定单元格时:=真 - Run-Time Error 1004 when attempting to lock cell in worksheet protected with UserInterface Only := True 仅选择具有数据的单元格,但跳过列中的第一个单元格 - Select only cells with data but skip the first cell in a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM