2014-10-07 170 views
3

我目前正在嘗試修改Visual Basic宏以僅將電子表格保存在已填充單元格的工作簿中。只保存已填充單元格的填充電子表格爲PDF

當前宏只是將整個16工作簿保存爲PDF,但最多9個這些工作表有時會保留未完成狀態,但仍然保存。

我希望宏自動檢查這些表是否已經填充,一旦點擊'保存'按鈕,然後繼續只保存填充(完整)工作表爲PDF。

我會大規模appricate任何幫助!

下面的代碼是宏保存整個工作簿時當前的工作方式。 (有一個IF語句檢查之前將其保存爲PDF格式。)

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 

回答

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 

我已經用我自己的示例工作簿測試過,然後嘗試將您的代碼示例細節合併到我的方法中。如果第一次發佈評論無效,我可能會提供幫助。

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 

答案可能會改變一點取決於你的填充紙張定義。你將不得不改變條件 「如果.UsedRange.Address <> 」$ A $ 1「,則」 一種可能的替代以上WorksheetFunction.CountA(範圍( 「A1:Z100」))<> 0

請讓我知道你是否需要任何協助的條件或代碼。

+0

謝謝,我已經試過了,但我似乎無法指定被定義爲填充的表單中的單元格,我還假設這就是您在說'取決於您的填充定義時的含義張? 9個薄片中的每一個與設置佈局相同,並且單元:D4:9,E10:15,F4:9,G10:15,H4:9,I10:15,J4:9,K10:15是這樣的單元需要完成。 – TPK 2014-10-07 14:27:10

+0

我認爲用** WorksheetFunction.CountA(範圍(「D4:D9,E10:E15,F4:F9,G10:G15,H4:H9,」)替換**。UsedRange.Address <>「$ A $ 1」 I10:I15,J4:J9,K10:K15「))= 48 **應該做這個工作... – ravishchhabra 2014-10-07 17:29:29

+0

請讓我知道如果能夠嘗試這種解決方案。如果您有任何問題,我會很樂意回答。 – ravishchhabra 2014-10-08 05:45:47