0 votes
in VBA by Beginner (58 points)

hello, guys 

I would code to "save as pdf" but only the filled data. I designed an invoice in sheet 1 but I would like the pdf to ignore the rows that are empty beginning  from A19:A32  with condition A19:D32 =" "  then delete the rows after I attached what I wanted in sheet1.

What I want is to press the macro button and create a sheet like this: 



by Expert (916 points)
Is your range a table?
You can simply loop and hide the empty rows in the desired column then export the sheet to pdf ..
by Beginner (58 points)
not a table  i design this  and i would vba macro automatically not manually

1 Answer

+1 vote
by Expert (916 points)
selected by
Best answer

Try this code

Sub Test()
    Dim c As Range, spath As String
    Application.ScreenUpdating = False
        With Worksheets("Sheet1")
            For Each c In .Range("A19:A32")
                If Application.Count(c.Resize(1, 4)) = 0 Then
                    c.EntireRow.Hidden = True
                End If
            Next c
            spath = ThisWorkbook.Path & "\Output.pdf"
            .ExportAsFixedFormat Type:=xlTypePDF, Filename:=spath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            .Range("A19:A32").EntireRow.Hidden = False
        End With
    Application.ScreenUpdating = True
End Sub


by Beginner (58 points)

 thanks so much  mr  YasserKhalil  the code does perfect work    i appreciate your help 

i would another demand  could  save just one page what contains the data  and delete the rest of pages 

by Expert (916 points)
You're welcome. The code is already exports just the sheet1 not the other sheets.
by Beginner (58 points)
I know  but what  I  ment  here  when  you  open  file  pdf  here  you see more  than  one  page  just  the  page  one contains  data   and  the rest are empty  according  this  I would  just  the  page  contains  data
by Expert (916 points)
Did you try to set the print area .. so as to print out specific range?
by Beginner (58 points)
Can  you  more  explaining  I don't  understand  you
by Expert (916 points)
Select the desired range that you would like to print >> Page layout tab >> Print Area >> Set Print Area .. This will make the exported PDF is limited to this area only

You can record macro and implement that into your code if you wish
by Beginner (58 points)
Thanks  I try  this

Welcome to wellsr Q&A
Ask any questions you have about VBA and Python and our community will help answer them. wellsr Q&A is the standalone question and answer platform for wellsr.com. If you have a question about one of our specific tutorials, please include a link back to the tutorial.

Getting Started
VBA Cheat Sheets (On Sale Now)

Looking for something else? Hire our team directly through ourVBA Help page, instead.

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.