gpt4 book ai didi

vba - 将工作簿中的每个工作表保存为单独的 pdf

转载 作者:行者123 更新时间:2023-12-04 21:55:19 25 4
gpt4 key购买 nike

我有一个工作簿,其中包含“工作表”中所有销售人员的所有销售,而在其他工作表中,工作表由销售人员编号(“41”、“51”、“88”等)命名。与他们的销售。我想要宏做的是获取每个工作表并保存为带有“工作表名称”和“文件名”的pdf

我的问题与这篇文章有关,但由于某种原因,我的版本没有正确保存 pdf。

excel vba - save each worksheet in workbook as an individual pdf

所以我想要的很简单:把每个工作表保存到它自己独特的 pdf 中。我遇到的问题是宏正在使用正确的文件名保存每个单独的工作表,但是当我打开 pdf 时,每个 pdf 的销售助理都是相同的。

这是代码:

Option Explicit

Sub WorksheetLoop()

Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim WS_Count As Integer
Dim I As Integer

' Set WS_Count equal to the number of worksheets in the active workbook.
Set wbA = ActiveWorkbook
WS_Count = wbA.Worksheets.Count
strPath = wbA.Path
strTime = Format(Now(), "yyyymmdd\_hhmm")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"

' Begin the loop.
For I = 1 To WS_Count

'replace spaces and periods in sheet name
strName = Replace(wbA.Worksheets(I).Name, " ", "")
strName = Replace(strName, ".", "_")

'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
myFile = strPath & strFile

Debug.Print myFile

'export to PDF if a folder was selected
If myFile <> "False" Then
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If

Next I

End Sub

如果您需要任何其他详细信息,请告诉我

最佳答案

您需要激活Activate在将每个工作表打印成 pdf 之前。尝试这个

 ' Begin the loop.
For Each wsA In wbA.Sheets

wsA.Activate
'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")

'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
myFile = strPath & strFile

Debug.Print myFile

'export to PDF if a folder was selected
If myFile <> "False" Then
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile

End If

Next

关于vba - 将工作簿中的每个工作表保存为单独的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46227726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com