作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我发现这段代码效果很好,但我需要将 1 张工作簿保存为 pdf。将文件格式更改为 xltypepdf
时不断出错.有人可以帮忙吗。
Sub DateFolderSave()
Application.DisplayAlerts = False
' Check for year folder and create if needed If
Len(Dir("C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date),
vbDirectory)) = 0 Then
MkDir "C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date)
End If
' Check for month folder and create if needed If
Len(Dir("C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date) & "\" &
MonthName(Month(Date), False), vbDirectory)) = 0 Then MkDir
"C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date) & "\" &
MonthName(Month(Date), False)
End If
' Check for date folder and create if needed If
Len(Dir("C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date) & "\" &
MonthName(Month(Date), False) & "\" & Format(Date, "dd.mm.yy"),
vbDirectory)) = 0 Then
MkDir "C:\Users\Christine\Desktop\Learner Lists LC\" & Year(Date) & "\" &
MonthName(Month(Date), False) & "\" & Format(Date, "dd.mm.yy")
End If
' Save File
ActiveWorkbook.SaveAs Filename:= _ "C:\Users\Christine\Desktop\Learner Lists
LC\" & Year(Date) & "\" & MonthName(Month(Date), False) & "\" & Format(Date,
"dd.mm.yy") & ".xlsm", _ FileFormat:=xlOpenXMLWorkbookMacroEnabled,
CreateBackup:=False
Application.DisplayAlerts = True
' Popup Message MsgBox "File Saved As:" & vbNewLine &
C:\Users\Desktop\Learner Lists LC\" & Year(Date) & _ "\" &
MonthName(Month(Date), False) & "\" & Format(Date, "mm.dd.yy") & ".xlsm"
End Sub
最佳答案
我做的第一件事(在弄清楚你的代码开始和结束的位置之后)是搜索和替换,使用 1 个变量和 1 个常量(而不是重复 8 次相同的文件夹名称)。
我的侦探技巧告诉我,您是 VBA 的新手:使用常量和变量是第一课,因为它们是“所有编码和开发”的基础。
但是不用担心,每个人都必须从某个地方开始。我重写了你的子程序:
Option Explicit
Sub ExportSheetToPDF()
'exports ActiveWorksheet to dated PDF
'Set openPDFwhenDone to TRUE to auto-open the PDF when finished
Const openPDFwhenDone = True
'Set constant basePath to the file save path
Const basePath = "C:\Users\Christine\Desktop\Learner Lists LC\"
'Declare variable pdfPath which for the complete path & filename
Dim pdfPath As String
'get the name of the "year" folder
pdfPath = basePath & Year(Date)
'if the "year" folder doesn't exist then create it
If Dir(pdfPath, vbDirectory) = "" Then MkDir pdfPath
'get the name of the "month" folder
pdfPath = pdfPath & "\" & Format(Month(Date), "00")
'if the "month" folder doesn't exist then create it
If Dir(pdfPath, vbDirectory) = "" Then MkDir pdfPath
'get the complete pdf filename
pdfPath = pdfPath & "\" & Format(Date, "yyyy-mm-dd") & ".pdf"
'export active worksheet as PDF to pdfPath
ActiveSheet.ExportAsFixedFormat xlTypePDF, pdfPath,,,,,, openPDFwhenDone
'make sure the pdf file was created
If Dir(pdfPath) = "" Then
'file not found
MsgBox "Something went wrong. (PDF wasn't created.)"
Else
'Success! Show success message (unless PDF was set to auto-open)
If Not openPDFwhenDoneThen MsgBox "File Saved As:" & vbLf & pdfPath
End If
End Sub
pdfPath
替换了。 .大小可能无关紧要,但效率和易读性才是重要的!
Sub ExportSheetToPDF_smaller()
Const basePath = "C:\Users\Christine\Desktop\Learner Lists LC\"
If Dir(basePath & Year(Date), vbDirectory) = "" Then MkDir basePath & Year(Date)
If Dir(basePath & Format(Date, "yyyy\\mm"), vbDirectory) = "" Then MkDir basePath & Format(Date, "yyyy\\mm")
ActiveSheet.ExportAsFixedFormat xlTypePDF, Format(Date, "yyyy\\mm\\yyyy-mm-dd.p\df"), , , , , , True
MsgBox "File Saved As:" & vbLf & Dir(basePath & Format(Date, "yyyy\\mm\\yyyy-mm-dd.p\df"))
End Sub
关于vba - 另存为 PDF,在年月文件夹下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47374844/
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 3 年前。 Improv
我看到另一篇文章解释了如何使用触发器来创建前缀 ID How to make MySQL table primary key auto increment with some prefix http:
我有一个使用 Spring Boot 的项目,如果给定的日期格式不正确,我想显示一个错误响应。正确的格式是 yyyy-MM (java.time.YearMonth) 但我想在有人发送 2020-13
我是一名优秀的程序员,十分优秀!