仅将已填充单元格的已填充电子表格另存为PDF

时间:2014-10-07 11:08:46

标签: excel excel-vba pdf save vba

我目前正在尝试修改Visual Basic宏,以便仅在已填充单元格的工作簿中保存电子表格。

当前的宏只是将整个16张工作簿保存为PDF,但这些工作表中最多有9张有时未完成,但仍然保存。

我希望宏能够自动检查这些工作表是否已填充,一旦单击“保存”按钮,然后继续仅将填好的(完整)工作表保存为PDF。

我会大量批评任何帮助!

以下代码是在保存整个工作簿时宏当前的工作方式。 (在将其保存为PDF之前,会进行IF语句检查。)

Sub SaveAsPDF()

    With ThisWorkbook.Sheets("COVERPage1PRINT")
        If (Len(.Range("C24")) = 0) Then
            MsgBox "Ensure Serial Number or Stamp number are filled."
            Exit Sub
        ElseIf (Len(.Range("H17")) = 0) Then
            MsgBox "Ensure Serial Number or Stamp Number are filled."
            Exit Sub

        Else
            ChDir _
            "P:\Cells\Spool & Sleeves Cell\Flow Plot Records\EFA\Saved EFA PDF Archive"
        fname = Sheets("COVERPage1PRINT").Range("H17")
        ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            "P:\Cells\Spool & Sleeves Cell\Flow Plot Records\EFA\Saved EFA PDF Archive\" & fname, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
            :=False, OpenAfterPublish:=True

        End If
    End With
End Sub

2 个答案:

答案 0 :(得分:1)

这应该可以胜任 (编辑代码)

Sub test1()

Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim test() As String
Dim i As Integer
Dim pdfpath As String
Dim sheets_to_be_checked() As Variant
Dim a As Boolean
pdfpath = ActiveWorkbook.Path 'YOU CAN ADD YOUR PDF SAVING LOCATION e.g. "C\Users\ABC\Desktop"

i = 0
sheets_to_be_checked = Array("Sheet1", "Sheet3")
Set wbBook = ActiveWorkbook

With ThisWorkbook.Sheets("COVERPage1PRINT")
    If (Len(.Range("C24")) = 0) Then
        MsgBox "Ensure Serial Number & Tag Number or Stamp number are filled."
        Exit Sub
    ElseIf (Len(.Range("H16")) = 0) Then
        MsgBox "Ensure Serial Number & Tag Number or Stamp Number are filled."
        Exit Sub
    ElseIf (Len(.Range("H19")) = 0) Then
        MsgBox "Ensure Serial Number & Tag Number or Stamp Number are filled."
        Exit Sub
    Else:
        For Each wsSheet In wbBook.Worksheets
            With wsSheet
                If IsInArray(wsSheet.Name, sheets_to_be_checked) Then
                    wsSheet.Activate
                    If WorksheetFunction.CountA(Range("D4:D9, E10:E15, F4:F9, G10:G15, H4:H9, I10:I15, J4:J9, K10:K15")) = 48 Then
                        ReDim Preserve test(i)
                        test(i) = wsSheet.Name
                        i = i + 1
                    End If
                Else:
                    ReDim Preserve test(i)
                    test(i) = wsSheet.Name
                    i = i + 1
                End If
            End With
        Next wsSheet
    End If
End With

ThisWorkbook.Sheets(test()).Select

ActiveSheet.ExportAsFixedFormat _
     Type:=xlTypePDF, _
     Filename:=pdfpath & "\ouput.pdf", _
     Quality:=xlQualityStandard, _
     IncludeDocProperties:=True, _
     IgnorePrintAreas:=False, _
     OpenAfterPublish:=True
End Sub


Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

答案可能会有所改变,具体取决于您对填充表格的定义。您必须更改条件“If .UsedRange.Address<>”$ A $ 1“Then” 上面的一种可能的替代方案是WorksheetFunction.CountA(Range(“A1:Z100”))<> 0

如果您对条件或代码有任何帮助,请与我们联系。

答案 1 :(得分:0)

这取决于'自动检查这些工作表是否已填充' 的具体含义。我的水晶球说每个工作表都有一个标题行,并且会被认为是“填充”。如果第一行下面有任何数据。在这种情况下,您可以遍历所有工作表并构造要选择的工作表名称数组。选择多个工作表后,PDF创建将位于ActiveSheet.ExportAsFixedFormat而不是ActiveWorkbook.ExportAsFixedFormat,并且只有选定的工作表才会包含在PDF中。

Dim w As Long, sWSs As String, vWSs As Variant
For w = 1 To Sheets.count
    With Sheets(w)
        If .Cells(1, 1).CurrentRegion.Rows.count > 1 Then _
            sWSs = sWSs & .Name & Chr(215)
    End With
Next w
If CBool(Len(sWSs)) Then
    vWSs = Split(Left(sWSs, Len(sWSs) - 1), Chr(215))
    Sheets(vWSs).Select
    ChDir _
        "P:\Cells\Spool & Sleeves Cell\Flow Plot Records\EFA\Saved EFA PDF Archive"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "P:\Cells\Spool & Sleeves Cell\Flow Plot Records\EFA\Saved EFA PDF Archive\" & fname, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Else
    MsgBox "Nothing to publish to PDF."
End If

我已经使用自己的示例工作簿对此进行了测试,然后尝试将您的代码示例细节合并到我的方法中。如果它第一次发布评论时没有工作,我可能会提供帮助。