选择所有"可见"工作表(打印到pdf)

时间:2016-03-16 21:21:13

标签: excel vba excel-vba

我在现有的应用程序中使用以下内容,这样可以将特定的工作表打印到pdf:

Sheets(Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")).Select

我试图打印一个只有我的"可见" 工作表的数组。

我已经搜索过一个解决方案,但无法用手指抓住它。

3 个答案:

答案 0 :(得分:3)

使用False参数添加到现有选择的简短方法:

Sub Test()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
 If ws.Visible Then ws.Select False
Next
End Sub

答案 1 :(得分:2)

试试这个

Option Explicit

Sub main()

ThisWorkbook.Sheets(GetVisibleWorksheetsNames(ThisWorkbook)).Select

End Sub


Function GetVisibleWorksheetsNames(wb As Workbook) As String()
Dim ws As Worksheet
Dim wsNames() As String
Dim iV As Long

With wb
    ReDim wsNames(1 To .Worksheets.Count)
    For Each ws In .Worksheets
        If ws.Visible Then
            iV = iV + 1
            wsNames(iV) = ws.name
        End If
    Next ws
    ReDim Preserve wsNames(1 To iV)
End With

GetVisibleWorksheetsNames = wsNames
End Function

答案 2 :(得分:0)

尝试使用此代码,它应该可以满足您的需求。

Sub SelectSheets()
    Dim myArray() As Variant
    Dim i As Integer
    Dim j As Integer
    j = 0
    For i = 1 To Sheets.Count
        If Sheets(i).Visible = True Then
            ReDim Preserve myArray(j)
            myArray(j) = i
            j = j + 1
        End If
    Next i
    Sheets(myArray).Select
End Sub