Save as PDF file but exclude blank pages (using macro)

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello...
I'm making an invoice in excel that has header on the top and detail of the items ordered on the table below.
The details of the items ordered varies widely for each month, so I won't know how long it's going to be.
It can be one page only, two pages, or even three pages.
I need to save it as PDF before sending to customer, so my question is that whether it's possible or not to save the file as PDF but excluding the blank pages?
The header will be there, but I need macro to see a certain cell on the table whether it's empty or not (there'll be formula in it, but the cell will be shown as empty).
If it's empty then that page won't be saved.

I attached the image.
Only the first and the second page that has items details on the table but not the third page.

If anyone can help, I would really appreciate it.
 

Attachments

  • Customer A.PNG
    Customer A.PNG
    90.7 KB · Views: 20
i don't know this is what you need or not:
VBA Code:
Sub ExportInvoice()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim ws As Worksheet
    Dim i As Long, j As Long
    Dim delCol As Range
    For Each ws In ThisWorkbook.Sheets
        i = ws.Cells(25, Columns.Count).End(xlToLeft).Column
        j = 1
        Do While j <= i - 13
            If IsEmpty(ws.Cells(26, j)) Then
                If delCol Is Nothing Then
                    Set delCol = ws.Range(Cells(1, j), Cells(51, j + 13))
                Else
                    Set delCol = Union(delCol, ws.Range(Cells(1, j), Cells(51, j + 13)))
                End If
            End If
            j = j + 14
        Loop
        If Not delCol Is Nothing Then delCol.Delete Shift:=xlToLeft
        ws.ExportAsFixedFormat Type:=xlTypePDF, fileName:=ThisWorkbook.Path & "\Invoice" & Format(Now, "yyyymmddhhmmss") & ".pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next ws
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
ah.... sorry, I misunderstood.
I changed it to vertical (3 invoice under each other).
 

Attachments

  • 1st page.PNG
    1st page.PNG
    42.8 KB · Views: 6
  • 2nd page.PNG
    2nd page.PNG
    37.1 KB · Views: 6
  • 3rd page.PNG
    3rd page.PNG
    28.1 KB · Views: 6
Upvote 0
i don't know this is what you need or not:
VBA Code:
Sub ExportInvoice()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim ws As Worksheet
    Dim i As Long, j As Long
    Dim delCol As Range
    For Each ws In ThisWorkbook.Sheets
        i = ws.Cells(25, Columns.Count).End(xlToLeft).Column
        j = 1
        Do While j <= i - 13
            If IsEmpty(ws.Cells(26, j)) Then
                If delCol Is Nothing Then
                    Set delCol = ws.Range(Cells(1, j), Cells(51, j + 13))
                Else
                    Set delCol = Union(delCol, ws.Range(Cells(1, j), Cells(51, j + 13)))
                End If
            End If
            j = j + 14
        Loop
        If Not delCol Is Nothing Then delCol.Delete Shift:=xlToLeft
        ws.ExportAsFixedFormat Type:=xlTypePDF, fileName:=ThisWorkbook.Path & "\Invoice" & Format(Now, "yyyymmddhhmmss") & ".pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next ws
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
May I know which cell address that you refer to?
I tried the code above and the blank page is still there.
or do I need to make it vertical as suggested instead of horizontal?
 
Upvote 0
i don't know this is what you need or not:
VBA Code:
Sub ExportInvoice()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim ws As Worksheet
    Dim i As Long, j As Long
    Dim delCol As Range
    For Each ws In ThisWorkbook.Sheets
        i = ws.Cells(25, Columns.Count).End(xlToLeft).Column
        j = 1
        Do While j <= i - 13
            If IsEmpty(ws.Cells(26, j)) Then
                If delCol Is Nothing Then
                    Set delCol = ws.Range(Cells(1, j), Cells(51, j + 13))
                Else
                    Set delCol = Union(delCol, ws.Range(Cells(1, j), Cells(51, j + 13)))
                End If
            End If
            j = j + 14
        Loop
        If Not delCol Is Nothing Then delCol.Delete Shift:=xlToLeft
        ws.ExportAsFixedFormat Type:=xlTypePDF, fileName:=ThisWorkbook.Path & "\Invoice" & Format(Now, "yyyymmddhhmmss") & ".pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next ws
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Thank you for your help.
I found another way by hiding the columns if it's empty.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top