在VBA中检查哪个Excel工作表处于活动状态(当前显示)

时间:2021-09-26 20:56:30

I'm looking for a method of checking if an excel sheet is currently active (currently shown). I'm interested in a synchronous method and not in an event.

我正在寻找一种方法来检查Excel工作表当前是否处于活动状态(当前显示)。我对同步方法感兴趣,而不是在事件中。

2 个解决方案

#1


22  

You could use set sh = ActiveSheet, or strShName = ActiveSheet.Name.
To test if sheet Xyz is active: If ActiveSheet.Name = "xyz" Then
You can also use If ActiveSheet.CodeName = "Sheet1" Then (VBE name)

您可以使用set sh = ActiveSheet或strShName = ActiveSheet.Name。要测试工作表Xyz是否处于活动状态:如果ActiveSheet.Name =“xyz”那么您还可以使用If ActiveSheet.CodeName =“Sheet1”然后(VBE名称)

#2


0  

Test for matching worksheet and workbook names.

测试匹配的工作表和工作簿名称。

Function IsActiveSheet(ByVal targetSheet As Worksheet) As Boolean
    IsActiveSheet = targetSheet.Name = ActiveSheet.Name And _
            targetSheet.Parent.Name = ActiveWorkbook.Name
End Function

It's a function. Place it in a module, and then call it from another procedure like this:

这是一个功能。将它放在一个模块中,然后从另一个程序调用它,如下所示:

Sub Test()
    Dim mySheetVar As Worksheet
    Set mySheetVar = ActiveWorkbook.Worksheets("Sheet1")

    ' Here's the function call which returns TRUE or FALSE.
    MsgBox IsActiveSheet(mySheetVar)
End Sub

#1


22  

You could use set sh = ActiveSheet, or strShName = ActiveSheet.Name.
To test if sheet Xyz is active: If ActiveSheet.Name = "xyz" Then
You can also use If ActiveSheet.CodeName = "Sheet1" Then (VBE name)

您可以使用set sh = ActiveSheet或strShName = ActiveSheet.Name。要测试工作表Xyz是否处于活动状态:如果ActiveSheet.Name =“xyz”那么您还可以使用If ActiveSheet.CodeName =“Sheet1”然后(VBE名称)

#2


0  

Test for matching worksheet and workbook names.

测试匹配的工作表和工作簿名称。

Function IsActiveSheet(ByVal targetSheet As Worksheet) As Boolean
    IsActiveSheet = targetSheet.Name = ActiveSheet.Name And _
            targetSheet.Parent.Name = ActiveWorkbook.Name
End Function

It's a function. Place it in a module, and then call it from another procedure like this:

这是一个功能。将它放在一个模块中,然后从另一个程序调用它,如下所示:

Sub Test()
    Dim mySheetVar As Worksheet
    Set mySheetVar = ActiveWorkbook.Worksheets("Sheet1")

    ' Here's the function call which returns TRUE or FALSE.
    MsgBox IsActiveSheet(mySheetVar)
End Sub