Macro for Creating PDF based on Cell Value

Chandresh

Board Regular
Joined
Jul 21, 2009
Messages
146
Hi Team,

Can anyone help me with a macro which will create diffrent PDF for each page in a sheet and name it based on cell value and save in folder on desktop.

Example : i have one sheet with 30 pages in it all page have similar format. i need a macro which will create diffrent pdf and name it based on cell X4(X4+30 for next Page) then after it should loop till 30th Page.

Thanks
Chan
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try this macro. You will see that it assumes the first page starts at row 1 (in the active sheet of the active workbook). It creates the PDF files in the "PDFs" subfolder on your Desktop; this subfolder is created if it doesn't exist.
Code:
Public Sub Create_PDF_For_Each_Page()

    Dim ws As Worksheet
    Dim lastRow As Long, pageStartRow As Long
    Dim page As Long
    Dim fileNameCell As Range
    Dim saveInFolder As String
    Dim PDFrange As Range
    Dim PDFfile As String
    
    saveInFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\PDFs\"
    If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
    If Dir(saveInFolder, vbDirectory) = vbNullString Then MkDir saveInFolder
    
    'Process pages on the active sheet in the active workbook
    
    Set ws = ActiveWorkbook.ActiveSheet
        
    With ws
        
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        pageStartRow = 1                        'first page starts at row 1
        Set fileNameCell = .Range("X4")         'cell containing file name (without .pdf extension) of first page
        
        'Save rows in each horizontal page break section as PDF file with file name in X4, X34, X64, etc.
        
        For page = 1 To .HPageBreaks.Count
            
            PDFfile = saveInFolder & fileNameCell.Value & ".pdf"
            Set PDFrange = .Rows(pageStartRow & ":" & .HPageBreaks(page).Location.Row - 1).EntireRow
            
            PDFrange.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfile, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            
            Set fileNameCell = fileNameCell.Offset(30)
            pageStartRow = .HPageBreaks(page).Location.Row
            
        Next
    
        If pageStartRow <= lastRow Then
        
            'Save rows after last horizontal page break as PDF file
            
            PDFfile = saveInFolder & fileNameCell.Value & ".pdf"
            Set PDFrange = .Rows(pageStartRow & ":" & lastRow).EntireRow
            
            PDFrange.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfile, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
            
        End If
    
    End With
    
    MsgBox "Created PDFs in " & saveInFolder
    
End Sub
 
Upvote 0
HI thanks for the code .

it created the first PDF however unable to create the others ,could you please help
it says
run-time error -2147024773 (8007007b)

 
Upvote 0
Which line causes that error? Click Debug on the error message and the errant line is highlighted in yellow.
 
Upvote 0

Forum statistics

Threads
1,213,490
Messages
6,113,957
Members
448,535
Latest member
alrossman

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