excel - 如何将文件保存到当年的文件夹?
问题描述
我有一个将工作表导出为 PDF 文件并将其保存在工作簿位置的宏。
如何将 PDF 文件保存到标记为“当前年份”的文件夹中,例如 2020?
如果当前年份没有文件夹,则将创建一个。
Dim StatementReports_Used As Range
Dim fullFileName As String
Dim saveLocation1 As String
Dim Y As Double
Dim X As Double
Dim year As Integer
year = Format(Date, "yyyy")
Y = DateValue(Now)
X = TimeValue(Now)
Dim dte As Date
dte = Now()
Dim numerical_date
numerical_date = Int(CDbl(dte))
Dim sourceDir As String
sourceDir = "C:\TextFolder\#19"
folder_exists = Dir(sourceDir & "\" & Str(year), vbDirectory)
fullFileName = "Text" & (StatementReports.Range("J20").Value) & "}" & "_" & Y & "_" & X
saveLocation1 = Dir(sourceDir & "\" & Str(year), vbDirectory) & "\" & fullFileName & ".pdf"
StatementReports.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=saveLocation1, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
If folder_exists = "" Then
MkDir sourceDir & "\" & Str(nowdate)
MsgBox "A Folder for the Current Year has been created."
Else
MsgBox "A folder for the Current Year Already exists. Your File will be saved to this."
End If
Application.ScreenUpdating = True
Application.CutCopyMode = False
解决方案
创建一个新文件夹很容易。
dim sourceDir as string
sourceDir = "C:\test_folder"
dim year as int
year = year(now())
' make the dir
mkdir sourceDir & "\" & str(year)
要将日期作为整数获取,只需将其转换
IE
dim dte as date
dte = now()
dim numerical_date
numerical_date = Int(CDbl(dte))
如果您想先检查文件夹是否存在。
folder_exists = Dir(sourceDir & "\" & str(year), vbDirectory)
If folder_exists = "" Then
MsgBox "The selected folder doesn't exist"
Else
MsgBox "The selected folder exists"
End If
那应该对你有用
编辑:
你问得很好的完整代码:)
Sub savePDF():
Dim dte As Date
Dim numericalDate As Integer
Dim sourceDir As String
Dim year As Integer
Dim reportWs As Worksheet
Dim folder_exists As String
Dim fullFileName As String
Dim pdfFileName As String
Dim folderPath As String
Dim filePart As String
'set worksheet as current
Set reportWs = Application.ActiveSheet
''OR
''set reportWs = worksheets("Worksheet_name")
'get year
year = Trim(Str(Format(Date, "yyyy")))
'get date
dte = Now()
'get numerical date
numerical_date = Int(CDbl(dte))
'source directory
sourceDir = "C:\TextFolder\#19"
'check if folder exists, if it doesnt them create a new directory
folder_exists = Dir(sourceDir & "\" & year, vbDirectory)
If folder_exists = "" Then
MkDir sourceDir & "\" & year
folder_exists = Dir(sourceDir & "\" & year, vbDirectory)
End If
'get folder path
folderPath = sourceDir & "\" & folder_exists
'get filename (I dont think you should use the DATE and TIME as you have as the characters are invalid) Please change below format as you see fit.
filePart = reportWs.Range("J20").Value
fullFileName = filePart & numerical_date & " " & Format(Now(), "dd-mm-yyyy HH_MM_SS")
'PDF save locaiton
pdfFileName = folderPath & "\" & fullFileName
'Save PDF
reportWs.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfFileName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
推荐阅读
- ruby-on-rails - Ruby on Rails 6 - 如何根据特定路线建模/隐藏视图?
- reactjs - showNotification api 不适用于 create-react-app
- postgresql - Postgres 的 NiFi 数据库连接
- c# - 如何使用c#将文本文件的一行读入数组,然后按索引获取元素
- python - 如何根据熊猫中的两列条件提取数据
- cookies - 在开玩笑测试时从 Cookies (js-cookie) 中检索数据
- python - altair 无法创建选择组合
- java - corda 实现通用保险库查询
- postgresql - 解释一个有限制和顺序的执行计划
- omnet++ - 对 SUMO、VEINS 和 OMNET++ 的怀疑