excel worksheets to pdf

How to create pdf file from each tab and name the file based on the cell "E3". the pdf file should pick column A to K and to the last active row in column K. below is the code that i found on internet


Sub createPDFfiles()
    Dim ws As Worksheet
    Dim try As String
    For Each ws In ActiveWorkbook.Worksheets
        On Error Resume Next

       
         try = ActiveWorkbook.Name & "-" & ws.Name

         try =  “D:\split the invoice\New folder\” & ActiveWorkbook.Name & "-" & ws.Name
       
        ws.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=try, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False
    Next ws
End Sub
NirvanalearnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,
pls try

Sub macro()

For Each sh In ActiveWorkbook.Sheets
    Set rng = sh.Range(SH.Range("A1"), sh.Range("K" & Cells.Rows.Count).End(xlUp))
    fPathFile = sh.Range("e3")
    rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPathFile, quality:=xlQualityStandard, _
        includedocproperties:=True, ignoreprintareas:=True, openafterpublish:=True
Next
End Sub

Open in new window

Regards
Kanti PrasadCommented:
Hi

The below will pick the value from E3 which is Cells(3,5) and name the pdf accordingly.

Sub createPDFfiles()
    Dim ws As Worksheet
    Dim try As String
    For Each ws In ActiveWorkbook.Worksheets
        On Error Resume Next
   
         try = ws.Cells(3, 5)
       '  try =  “c:\” & ws.Cells(3, 5)
       Set AtoK = ws.Range(ws.Range("A1"), ws.Range("K" & Cells.Rows.Count).End(xlUp))
        AtoK.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=try, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False
    Next ws
End Sub
NirvanalearnerAuthor Commented:
Hi Thank you very much for a very quick reply for some reason i am not able to get all the rows in the pdf. am i doing something wrong?

pdf should have all the contents from Columns  A column k and all the active row starting from A1 in the attached file i need the pdf to have contents from cell A1 to L 55
sample.xlsx
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Kanti PrasadCommented:
Hi

I am not sure what you want but change the K to L55 so you should have all the data from A1 to L55
NirvanalearnerAuthor Commented:
@kanti
sorry if i was not able to articulate it properly from row 1 to 9 I have no data in columns A and B from row 9 i have data in rows

something like below code but do not know how to make it work

Sub createPDFfiles()
    Dim ws As Worksheet
    Dim try As String
    For Each ws In ActiveWorkbook.Worksheets
        On Error Resume Next
   
         try = ws.Cells(3, 5)
       '  try =  “c:\” & ws.Cells(3, 5)
       Set AtoK = ws.Range(ws.Range("A1"), ws.Range("L8" & Cells.Rows.Count))
       Set AtoK = ws.Range(ws.Range("A9"), ws.Range("L" & Cells.Rows.Count).End(xlUp))
        AtoK.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=try, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False
    Next ws
End Sub

Open in new window


sample.png
Kanti PrasadCommented:
Hi

So you just need the invoice no and the rows from A9 to end of sheet  right?
Just remove this line
 Set AtoK = ws.Range(ws.Range("A1"), ws.Range("L8" & Cells.Rows.Count))
NirvanalearnerAuthor Commented:
Hi

I need the data from A1 to J1537
Rgonzo1971Commented:
pls try

 Set AtoK = ws.Range(ws.Range("A1"), ws.Range("J" & Cells.Rows.Count).End(xlup))

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NirvanalearnerAuthor Commented:
Thank you thank you.. super.. works beautifully
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.