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

久未见 提交于 2020-01-13 10:49:07

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/8801503/how-to-fetch-multiple-selected-sheets-handles-in-a-workbook-using-excel-vba-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!