3

我正在编写一个 VBA 代码,以将 excel 中的一些工作表导出到相同的 PDF。我的 excel 文件中有几个图表表,每个表的名称都以“(name)_Chart”结尾。我想将所有名称以图表结尾的工作表导出到一个 PDF 文件。这是我正在尝试编写的代码。

Sub FindWS()
        'look if it at least contains part of the name
        Dim s As Worksheet
        Dim strPath As String

        strPath = ActiveWorkbook.Path & "\"

        For Each s In ThisWorkbook.Sheets
            If InStr(1, s.Name, Chart) Then
                s.Activate
                ActiveSheet.ExportAsFixedFormat xlTypePDF, strPath & s.Name & ".pdf"
                Exit Sub
            End If
        Next s
End Sub

此代码不仅限于导出图表,而是导出整个工作簿。谁能帮我弄清楚我的代码中缺少什么。

谢谢!

修改后的代码:

Sub FindWS()
'look if it at least contains part of the name
Dim s As Worksheet
Dim strPath As String

strPath = ActiveWorkbook.Path & "\"

For Each s In ThisWorkbook.Worksheets
      If InStr(1, s.Name, "Chart") = 0 Then
          ' Hide the sheet so it is not exported as PDF
          s.Visible = False
             End If
Next s
          With ActiveWorkbook
          .ExportAsFixedFormat xlTypePDF, strPath & "TEST.pdf"
                End With

结束子

4

2 回答 2

2

我很惊讶您的代码首先在运行:) 您实际上应该遇到错误run time error '13', type mismatch

Sheets在 Excel中Worksheets是两个不同的东西

Worksheets集合是指定或活动工作簿中所有 Worksheet 对象的集合。每个 Worksheet 对象代表一个工作表。而Sheets另一方面,该集合不仅包括工作表集合,还包括其他类型的工作表,包括图表工作表、Excel 4.0 宏工作表和 Excel 5.0 对话框工作表。

因此,如果您将对象声明为Worksheet

Dim s As Worksheet

然后确保在循环时循环正确的集合

For Each s In ThisWorkbook.Worksheets

并不是

For Each s In ThisWorkbook.Sheets

否则你会得到一个run time error '13', type mismatch

跟进(基于评论)

@Siddharth:1. 是的,我想导出以名称“Chart”结尾的图表工作表。2. 我想要一个 PDF 中的所有这些图表,并且 PDF 的名称应该是“原始”文件名。(我必须将最终的 PDF 文件保存在不同的位置,以免文件重叠。) - 以数据为中心

Option Explicit

Sub Sample()
    Dim ws As Object
    Dim strPath As String, OriginalName As String, Filename As String

    On Error GoTo Whoa

    '~~> Get activeworkbook path
    strPath = ActiveWorkbook.Path & "\"
    '~~> Get just the name without extension and path
    OriginalName = Left(ActiveWorkbook.Name, (InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1))
    '~~> PDF File name
    Filename = strPath & OriginalName & ".pdf"

    '~~> Loop through Sheets Collesction
    For Each ws In ActiveWorkbook.Sheets
        '~~> Check if it is a Chart Sheet and also it ends in "Chart"
        If ws.Type = 3 And UCase(Right(Trim(ws.Name), 5)) = "CHART" Then
            ws.Visible = True
        Else
            ws.Visible = False
        End If
    Next ws

    '~~> Export to pdf
    ActiveWorkbook.ExportAsFixedFormat xlTypePDF, Filename

LetsContinue:
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub
于 2013-02-18T05:12:28.683 回答
0

此代码将查看所有工作表。如果工作表名称不匹配,它将隐藏它。完成后,它将所有可见工作表导出到一个 PDF 中。确保您以后不要保存 Excel 文件,否则工作表将保持隐藏状态。

当然,此代码未经测试,因此如果您有问题请回问(或尝试自行解决,因为您可能会学到一些东西)

Sub FindWS()
    'look if it at least contains part of the name
    Dim s As Worksheet
    Dim strPath As String

    strPath = ActiveWorkbook.Path & "\"

    For Each s In ThisWorkbook.Sheets
        If InStr(1, s.Name, "Chart") = 0 Then
              ' Hide the sheet so it is not exported as PDF
              s.Visible = False 
        End If
    Next s

    ' Export all sheets as PDF         
    ActiveSheet.ExportAsFixedFormat xlTypePDF, strPath & "TEST.pdf"
End Sub
于 2013-02-18T04:35:34.460 回答