Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50070

Run-time Error 9 when creating PDFs

$
0
0
Hi,

I use the following code to create PDFs from worksheets...Aber creasting the first PDF sucessfully, the error box "Runtime error 9. suscript out of range" pops up before the second pdf is created.

Any ideas what I did wrong? (Sry in case it is an obvious soultion, tried more or less everything before this post)...When clicking debug, Worksheets(sheetName).PageSetup.PrintArea = "" is marked yellow.


Code:

Sub pdfandemail()

For Each c In Range("clients")
    Dim lLR As Long
    Dim sheetName As String
    sheetName = c.Value
    Dim lNumberOfRowsPerSheet As Integer
    Worksheets(sheetName).PageSetup.PrintArea = ""
    Worksheets(sheetName).ResetAllPageBreaks
    lNumberOfRowsPerSheet = 46    '<---- Change as required
   
    lLR = Range("B" & Rows.count).End(xlUp).Row - 1
    'lLR = 60
   
   
    Worksheets(sheetName).PageSetup.PrintArea = "$B$2:$AB$100"
    PrintTitleRows = "A8:S9"
   
   
    For l = lNumberOfRowsPerSheet + 1 To ActiveSheet.UsedRange.Rows.count Step lNumberOfRowsPerSheet
        Worksheets(sheetName).HPageBreaks.Add Before:=Range("A" & l)
    Next
   
    Worksheets(sheetName).PageSetup.Zoom = 50
   
    Worksheets(sheetName).Range("A3:S" & lLR).ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:= _
            "\\YYY\XXXX\Desktop\XXXX\" & sheetName & ".pdf", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
   
   

Next c


End Sub



Cheers,
Konstantin

Viewing all articles
Browse latest Browse all 50070

Trending Articles