如何使用 Excel VBA API 在工作簿中获取多个选定的工作表句柄

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8801503/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:53:10  来源:igfitidea点击:

How to fetch multiple selected sheets handles in a workbook using Excel VBA API

excel-vbavbaexcel

提问by hjindal

There is a way to select multiple Excel sheets and then do some action on them like Print. However given a workbook, how do i get to know which sheets are selected. There is a vba property Application->ActiveSheet which gives us current active sheet, but i couldn't find any way to get multiple sheets for this.

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

回答by Siddharth Rout

Is this what you want?

这是你想要的吗?

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

Sid

锡德