简体   繁体   中英

Excel export last row to pdf

ID          Employee    Date       Base 
1234        me          11/03/2015  UK 
9999        U           11/03/2015  UK  
1111        Us          11/03/2015  UK  

Hi I have the above sample table that is updated each time a user saves data from a user form. I need to be able to export the most recent row to a PDF. I have written the following code but I am only getting data in the last row of column A. How can I get the whole row to be exported?

Sub PDFActiveSheet()

Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler


'Make Sheet2 active
Sheet2.Activate

Set ws = ActiveSheet

'enter name and select folder for file

' start in current workbook folder
strFile = Replace(Replace(ws.Name, " ", ""), ".", "_") _
        & "_" _
        & Format(Now(), "yyyymmdd\_hhmm") _
        & ".pdf"
strFile = ThisWorkbook.Path & "\" & strFile

myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
    FileFilter:="PDF Files (*.pdf), *.pdf", _
    Title:="Select Folder and FileName to save")

If myFile <> "False" Then
 ws.Cells(Rows.Count, "A").End(xlUp).ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

MsgBox "PDF file has been created."
End If

exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler

End Sub

Thanks in advance for any help!

Here is one way to export the filled part of the last row as PDF

Sub PDFMaker()
    Dim ws As Worksheet, M As Long, N As Long
    Dim myFile As String
    myFile = "qwerty"
    Set ws = ActiveSheet
    N = ws.Cells(Rows.Count, "A").End(xlUp).Row
    M = ws.Cells(N, Columns.Count).End(xlToLeft).Column

    ws.Range(ws.Cells(N, 1), ws.Cells(N, M)).ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM