Excel 将用户从列表框内容保存为PDF文件
下面是分配给我的“生成报告”命令按钮的宏,用于将活动工作表保存为pdf文件。我尝试使用此宏将我的userform列表框的内容保存为PDF格式。这是可以实现的吗Excel 将用户从列表框内容保存为PDF文件,excel,vba,pdf,listbox,report,Excel,Vba,Pdf,Listbox,Report,下面是分配给我的“生成报告”命令按钮的宏,用于将活动工作表保存为pdf文件。我尝试使用此宏将我的userform列表框的内容保存为PDF格式。这是可以实现的吗 Sub PDFActiveSheet() Dim ws As Worksheet Dim strPath As String Dim myFile As Variant Dim strFile As String On Error GoTo errHandler Set ws = ActiveSheet 'enter name and
Sub PDFActiveSheet()
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler
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.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
With ws.PageSetup
.CenterHeader = "Asset List"
.Orientation = xlPortrait
.Zoom = True
.FitToPagesTail = False
.FitToPagesWide = 1
End With
MsgBox "PDF file has been created."
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
您需要添加一个帮助表,以便在向列表框(Me.ListBox1.List(ListBox1.ListCount-1,X-1)=ws.Cells(i,X))追加内容时,将相同的信息粘贴到帮助表中,以维护列表,从而使您能够将该帮助表PDF格式 类似这样的东西应该可以让您在For X循环中到达那里: 请注意,在您的代码中,您正在合并一个更大的列表,因此仅收集该合并列表的有效方法是将其放在自己的位置,以便以后使用 您可以将循环添加到PDF宏中,以说明此其他工作表,例如:
Dim i as long, arr as variant
arr = array("Sheet1","Sheet3")
For i = lbound(arr) to ubound(arr)
With Sheets(arr(i))
'PDFing macro
End with
Next i
Edit1: 希望更清楚一点(请注意,您可能需要在工作簿中添加一张工作表,因为我随意使用Sheet3):
简短回答:是的。列表框内容的位置在哪里?如果这是excel中的一张工作表,那么您可以将该工作表另存为pdf,如果它是通过代码生成的,则需要将其发布到可读位置,并将其另存为pdf.Hi Cyril。列表框的格式为userform。当我单击搜索框时,内容显示在列表框中。它是通过代码生成的。好吧,鉴于列表框很可能是由代码中的数组生成的,填充userform1.listbox1,您应该能够将该数组附加到工作表中,然后。移动该工作表和pdf文件。您能发布代码说明列表框是如何填充的吗?嗨,Cyril,我已经在问题中添加了如何填充我的列表框对不起,我没有完全理解答案中的第一个代码。我们谈论的是哪个for X循环?@Kev填充列表框的循环,包括我文章中斜体的代码行(从您的代码中复制)。你的第二个for X只是一个计数,因此你没有数据放入助手工作表。工作表(arr(i))抛出一个错误,上面写着“这是我们的范围”@Kev你是否更改了数组以匹配你想要查看的内容(实际工作表名)?arr=array(“Sheet1”、“Sheet3”)就是一个例子。很抱歉,我仍然不知道如何将您提供的第一个代码放入X循环中
With Sheets("Sheet3")
.Cells(.Rows.Count,1).End(xlUp).Row,1).Value = ws.Cells(i, X)
End With
Dim i as long, arr as variant
arr = array("Sheet1","Sheet3")
For i = lbound(arr) to ubound(arr)
With Sheets(arr(i))
'PDFing macro
End with
Next i
For X = 1 To 8
Me.ListBox1.List(ListBox1.ListCount - 1, X - 1) = ws.Cells(i, X)
With Sheets("Sheet3")
.Cells(.Rows.Count,1).End(xlUp).Row,1).Value = ws.Cells(i, X)
End With
Next X