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

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

我有一个工作表,在AI列有工作簿中的所有工作表的列表。 列B有一个dropdown列表,用户将selectY或N来指示他们是否打印该表格。

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

我的问题是,当我试图将这个string名称分配给一个数组,我得到一个错误的下标超出范围。 我添加了我的消息框的屏幕快照,显示最终string的样子和错误的屏幕截图。

在这里输入图像说明

任何帮助将不胜感激!

 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 

错误消息 消息框

试试看,代码是未经testing的。

 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