Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 根据用户选择一次打印多个工作表_Vba_Excel - Fatal编程技术网

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

Vba 根据用户选择一次打印多个工作表,vba,excel,Vba,Excel,我需要一种方法,能够打印多个工作表作为一项工作,根据什么工作表的用户想要打印 我有一个工作表,其中a列列出了工作簿中的所有工作表。B列有一个下拉列表,用户可以在其中选择Y或N来指示是否要打印该页 我的代码应该做的是将B列中有Y的所有工作表作为一个作业打印。我有一个名为FirstSheetPrint的单元格引用,它复制了工作表列表中第一个工作表的名称,该工作表在B列中有一个Y。然后我取该名称并在其周围添加“'s”,以便以后可以将其用作工作表名称。然后我循环遍历所有有Y和add的行”,并将工作表名称

我需要一种方法,能够打印多个工作表作为一项工作,根据什么工作表的用户想要打印

我有一个工作表,其中a列列出了工作簿中的所有工作表。B列有一个
下拉列表
,用户可以在其中选择Y或N来指示是否要打印该页

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

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

任何帮助都将不胜感激

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

尝试一下,代码未经测试

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

检查图纸名称或范围名称是否正确。检查图纸名称是否正确。我使用复制/粘贴来确保所有内容100%准确。我假设问题在于
工作表(数组(SheetsToPrint))上的实际语法。Printout
code这只是单独打印所有工作表。我需要它作为一个作业全部打印出来,因为我们打印到pdf,所以如果我使用这种方法,那么每个工作表都是一个单独的文件。请参阅我的评论:“我认为这也应该作为一个1行程序工作”工作表(MyArr)。打印输出是的,我只是在运行它之后注意到了这一点。它可以工作,但出于某种原因,它仍然可以分为3个工作。我打印了16张工作表。它先打印10,然后打印4,然后打印2。但它是有效的!非常感谢你!