根据用户选择一次打印多个工作表

时间:2016-05-25 19:15:05

标签: excel vba excel-vba

我需要一种能够根据用户想要打印的工作表将多个工作表打印为一个作业的方法。

我有一个工作表,在A列中我有一个工作簿中所有工作表的列表。 B列有一个dropdown列表,用户可以选择Y或N来表示是否要打印该表。

我的代码应该做的是打印所有在B列中具有Y作为一个作业的工作表。我有一个名为FirstSheetPrint的单元格引用,它复制了第B列中包含Y的工作表列表中第一个工作表的名称。然后我取出该名称并在其周围添加“s”以便可以使用它稍后作为工作表名称。然后我循环遍历所有具有Y的行并将“s”和工作表名称添加到单个字符串(字符串随着循环继续而不断增长)。

我的问题是,当我尝试将这个Sheet名称字符串分配给一个数组时,我得到一个错误的下标超出范围。我添加了一个消息框的屏幕截图,显示了最终字符串的样子以及错误的屏幕截图。

enter image description here

非常感谢任何帮助!

Sub PrintAllSheets()

Application.ScreenUpdating = False
Application.EnableEvents = False

Dim DoesPrint As Boolean
Dim SheetsToPrint As String
Dim SheetCount As Integer
Dim StartCount As Integer
Dim StartRange As String
Dim totalString As String

DoesPrint = Application.Dialogs(xlDialogPrinterSetup).Show

If DoesPrint = False Then
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
Else
End If

SheetsToPrint = Sheets("Printing").Range("FirstSheetPrint").Value
SheetCount = Sheets("Printing").Range("SheetsToPrintCount").Value
StartCount = 4

If SheetsToPrint = "N" Then
    MsgBox "Please select which sheets you would like to print"
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Exit Sub
Else
    SheetsToPrint = ("""" & SheetsToPrint & """")
    For i = 1 To SheetCount

        If Sheets("Printing").Range("N" & StartCount).Value > 0 Then
            SheetsToPrint = (SheetsToPrint & ", " & """" & Sheets("Printing").Range("L" & StartCount).Value & """")
            StartCount = StartCount + 1
        Else
            StartCount = StartCount + 1
        End If
    Next i

    MsgBox ("We will print: " & SheetsToPrint)
End If

Sheets(Array(SheetsToPrint)).PrintOut


Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

Error Message Message Box

1 个答案:

答案 0 :(得分:2)

尝试一下,代码未经测试。

Sub PrintAllSheets()
Application.ScreenUpdating = False
Application.EnableEvents = False

Dim DoesPrint       As Boolean
Dim SheetsToPrint   As String
Dim SheetCount      As Integer
Dim StartCount      As Integer
Dim MyArr()         As String
Dim StartRange      As String
Dim totalString     As String
Dim i               As Long

DoesPrint = Application.Dialogs(xlDialogPrinterSetup).Show

If DoesPrint = False Then
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Exit Sub
End If

SheetsToPrint = Sheets("Printing").Range("FirstSheetPrint").Value
SheetCount = Sheets("Printing").Range("SheetsToPrintCount").Value
StartCount = 4

If SheetsToPrint = "N" Then
    MsgBox "Please select which sheets you would like to print"
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Exit Sub
Else
    'SheetsToPrint = ("""" & SheetsToPrint & """") 'Not sure if this is needed
    For i = 1 To SheetCount
        If Sheets("Printing").Range("N" & StartCount).Value > 0 Then
            SheetsToPrint = (SheetsToPrint & "," & Sheets("Printing").Range("L" & StartCount).Value)
            StartCount = StartCount + 1
        Else
            StartCount = StartCount + 1
        End If
    Next i
    MsgBox ("We will print: " & SheetsToPrint)
End If

'Split the string into an array
MyArr = Split(SheetsToPrint, ",")

'Print the array
Sheets(MyArr).PrintOut

Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub