忽略数组中隐藏的表格

时间:2018-01-22 09:52:22

标签: vba excel-vba excel

我目前正在制作一个工作簿,允许用户为不同的部门打印不同的报告。

对于不同的阶段,工作簿具有同一工作表的多个副本,用户可能只需要使用8个阶段中的1个或2个阶段。

我添加了一个表单,该表单在按下打印后出现,允许用户选择要打印的报表,然后在打印前选择相关的表格。

这是我试图开始工作的代码,它忽略了隐藏的工作表,但只打印当前工作表而不是数组中可见的工作表。

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 And IsInArray(Sheets(i).Name, Array("Sheet1", "Sheet2", "Sheet3")) Then
       ReDim Preserve myArray(j)
           myArray(j) = Sheets(i).Name
           j = j + 1
       End If
    Next i
    Sheets(myArray).Select
End Sub

Function IsInArray(stringToBeFound As String, arr As Variant)
    IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function

提前感谢您的帮助。

马特

1 个答案:

答案 0 :(得分:0)

请看一下这段代码。

Sub SelectSheets()

    Const ExcludedSheets As String = "Sheet1,Sheet2,Sheet3"

    Dim SelectedSheets() As String
    Dim Ws As Worksheet
    Dim i As Integer

    With ActiveWorkbook
        ReDim SelectedSheets(.Worksheets.Count)
        i = -1
        For Each Ws In .Worksheets
            If InStr(1, ExcludedSheets, Ws.Name, vbTextCompare) = 0 Then
                If Ws.Visible = xlSheetVisible Then
                    i = i + 1
                    SelectedSheets(i) = Ws.Name
                End If
            End If
        Next Ws

        If i > -1 Then
            ReDim Preserve SelectedSheets(i)
            .Worksheets(SelectedSheets).Select
        End If
    End With
End Sub

下面的代码会打印工作表而不是选择它们。

Sub PrintSelectedSheets()
    ' 24 Jan 2018

    Const ExcludedSheets As String = "Sheet1,Sheet2,Sheet3"

    Dim Ws As Worksheet

    With ActiveWorkbook
        For Each Ws In .Worksheets
            If InStr(1, ExcludedSheets, Ws.Name, vbTextCompare) = 0 Then
                With Ws
                    If .Visible = xlSheetVisible Then .PrintOut
                End With
            End If
        Next Ws
    End With
End Sub