使用VBA激活多个工作表以进行导出

时间:2017-03-18 03:39:49

标签: excel-vba vba excel

我正在尝试编写一个例程,该例程从Excel VBA表单中获取用户对可用工作表的选择,并将它们导出到一个PDF文档中。我计划使用它导出到Word和PowerPoint例程。我已经尝试过Stack Overflow中的一些想法,据说这些想法都有效。我没有运气。我也尝试过其他来源的各种想法......也许我对显而易见的事情视而不见。

我尝试使用数组(arrSheets,仍然在代码中,我希望也许我仍然可以使用它。)。它填充了表单对象,但是使用动态数组和redim命令对我来说并不起作用。

我所拥有的代码在“ActiveSheet.ExportAsFixedFormat ...”行之前似乎运行良好。此时我得到“应用程序定义或对象定义的错误(运行时错误1004)”

以下代码从VBA表单上的命令按钮触发...

Private Sub cmdExport_Click()
    'Find the selected documents from the form's checkboxes and send to the export routine
Dim intArrayCounter, intSelectionNum As Integer
Dim bolFound As Boolean
Dim ctrl As control
Dim arrSheets(1 To 6) As Variant ' the array to hold the worksheet objects...

    intSelectionNum = 0 ' which checkbox is it
    intArrayCounter = 1 ' array index
    bolFound = False    ' was a checked box found?

    For Each ctrl In frmToPDF.Controls
        If TypeName(ctrl) = "CheckBox" Then
            intSelectionNum = intSelectionNum + 1 ' set the selection number
            If ctrl.Value = True Then
                bolFound = True '  found a selection set the flag to true

                    Set arrSheets(intArrayCounter) = Sheets(intSelectionNum)
                    ThisWorkbook.Sheets(intSelectionNum).Select

                ' increment the counter
                intArrayCounter = intArrayCounter + 1
            End If
        End If
    Next

    'Sheets(arrSheets).Select <---remmed out cause this throws an error

    If bolFound = False Then   ' if there is Nothing selected send a message, or do the deal...
        Call MsgBox("There is nothing selected to export!", vbOKOnly, "Nothing selected...")
    Else
        frmExport.Caption = "Processing the document...Please be patient!"

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\test.pdf", Quality:=xlQualityStandard, IgnorePrintAreas:=False, DisplayFileAfterPublish:=True
        'the above Activesheet routine throws "Application-defined or Object-defined error  (Run-time error 1004)"
    End If

    ThisWorkbook.Sheets(intSheet).Select

End Sub

3 个答案:

答案 0 :(得分:0)

我可以为您的ExportAsFixedFormat语句考虑失败的唯一原因是您的安装中未启用选项DisplayFileAfterPublish(如果您未安装Acrobat Reader,则通常是这样)。您可以在尝试手动导出到PDF时进行检查,必须禁用"open file after publishing"选项。

尝试删除参数DisplayFileAfterPublish

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\test.pdf", _
    Quality:=xlQualityStandard, IgnorePrintAreas:=False

至于你帖子中的其他问题,我没有看到将Worksheet个对象放入数组中有什么问题。但是Sheets(arrSheets).Select是不正确的,而且不需要过时。它格式不正确,因为arrSheets参数是一个包含表格引用的数组,而它应该是indices的数组,这就是全部。

另外我不知道为什么Redim 没有为您效用,因为您没有说明您是如何使用它的。

答案 1 :(得分:0)

我无法使用“ActiveSheet.ExportAsFixedFormat”。但我在StackOverflow上的其他地方找到了这个答案,它的效果非常好,而且代码非常少: Use a method on multiple sheets simultaneously without using ActiveSheet or Select 谢谢大家的帮助......

答案 2 :(得分:-1)

这对我有用。由于您根据表格复选框的tabindex选择了工作表编号,所以选择的工作表可能不是您想要的工具,因此存在缺陷(也存在于OP的原始版本中)。你如何纠正这个问题取决于你,这超出了范围。

Private Sub CommandButton1_Click()
    Dim intSelectionNum As Integer
    Dim bolFound As Boolean
    Dim ctrl As Control

    bolFound = True     ' was a checked box found? True = NO
    intSelectionNum = 0 ' which checkbox is it

    For Each ctrl In FrmToPDF.Controls
        If TypeName(ctrl) = "CheckBox" Then
            intSelectionNum = intSelectionNum + 1 ' set the selection number
            If ctrl.Value = True Then
                ThisWorkbook.Sheets(intSelectionNum).Select bolFound
                bolFound = False ' from now on we extend the selection
            End If
        End If
    Next

    If bolFound = True Then   ' if there is Nothing selected send a message, or do the deal...
        MsgBox "There is nothing selected to export!", vbOKOnly, "Nothing selected..."
    Else
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\test.pdf", Quality:=xlQualityStandard, IgnorePrintAreas:=False
    End If
End Sub

这里发生了什么:我正在重载bolFound以设置可选的Worksheets.Select参数 - True表示替换当前选择,False表示扩展当前选择。通过将bolFound作为True启动,我无效任何现有选择。然后将bolFound设置为False,这将扩展后续工作表的选择范围。我不需要数组,因为我正在为我管理选择。

注意:ThisWorkbook.Sheets(intSelectionNum).Select一次只能选择一张(默认为True),因此无论检查了多少个盒子,OP的代码一次只会导出一张。

我安装了Acrobat Readeer并且, DisplayFileAfterPublish:=True为我工作,但我在示例中省略了该部分。

奖金讨论:与OP的代码一样,该示例选择“Sheets”而不是“Worksheets”。这允许它导出例如图表,Excel4宏和对话框等等(尽管现在只有图表会引起大多数人的兴趣)。如果我将“Sheets”更改为“Worksheets”,它将只导出工作表。

强制免责声明:这是将所选工作表导出为PDF的示例代码。它尚未经过广泛测试,并不是一个简单的解决方案。这个对我有用。根据您的系统,您可能需要进行调整。

编辑添加:以找出选择了哪些工作表(例如,构建您的数组以用于其他目的),您可以在For / Next循环完成后插入:

dim sht as Object
For Each sht In Application.ActiveWindow.SelectedSheets
    Debug.Print sht.Name
Next
相关问题