Macro for Creating PDF based on Cell Value
Results 1 to 6 of 6

Thread: Macro for Creating PDF based on Cell Value
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Jul 2009
    Posts
    99
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Macro for Creating PDF based on Cell Value

    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

  2. #2
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,807
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Macro for Creating PDF based on Cell Value

    Are the pages separated by horizontal page breaks?

  3. #3
    Board Regular
    Join Date
    Jul 2009
    Posts
    99
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Macro for Creating PDF based on Cell Value

    Yes

  4. #4
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,807
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Macro for Creating PDF based on Cell Value

    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

  5. #5
    Board Regular
    Join Date
    Jul 2009
    Posts
    99
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Macro for Creating PDF based on Cell Value

    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)


  6. #6
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,807
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Macro for Creating PDF based on Cell Value

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

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •