Excel将最后一行导出为pdf

时间:2015-03-12 17:28:45

标签: excel excel-vba vba

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

您好我每次用户从用户表单保存数据时都会更新上面的示例表。我需要能够将最新的行导出为PDF。我编写了以下代码,但我只在A列的最后一行获取数据。如何才能导出整行?

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

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

以下是将最后一行的填充部分导出为 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