有一种方法可以选择多个Excel工作表,然后对它们执行一些操作,例如“打印”。但是给定一个工作簿,我如何知道选择了哪些工作表。有一个vba属性Application-> ActiveSheet,它为我们提供了当前的活动工作表,但是我找不到任何方法来为此获得多个工作表。

最佳答案

这是你想要的吗?

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim SelectedSheets() As String
    Dim n As Long, i As Long

    n = 0
    For Each ws In ActiveWindow.SelectedSheets
        ReDim Preserve SelectedSheets(n)
        SelectedSheets(n) = ws.Name
        n = n + 1
    Next

    For i = LBound(SelectedSheets) To UBound(SelectedSheets)
        '~~> This will give you the list of selected sheets
        Debug.Print SelectedSheets(i)
    Next i

    '~~> The collection can also be used as below
    'Sheets(SelectedSheets).Copy
    'Sheets(SelectedSheets).Select   ' e.g., to re-select them later
End Sub


席德

07-26 02:07