将文件保存为日期
Saving a file as a date
我正在导入每日数据并对其进行处理。我想将文件保存为从A4中提取的数据的日期。
数据最初以 CSV 文件的形式出现,数据格式为 27/06/2020。我在操作过程中重新格式化了数据,但无论如何,当我保存文件时,我得到了
Runtime error 1004 Microsoft Excel cannot access the file
"C:\Users\steve\Desktop\D2D75130"
我不清楚是否(以及如何)将其更改为文本格式以便保存 - 但我喜欢将日期保留为日期格式以帮助我进行数据分析的想法。或者也许我应该在日期前添加一个文本字符串,即“销售额(然后是日期)”——如果这样行得通吗?这是我现有的代码:
' This is to save the file on the desktop using the date (content of cell A4) as the filename
' It also saves the file with the name 'Latest Report' in the Reports Folder
Sub FileNameAsCellContent()
Dim FileName As String
Dim Path As String
Application.DisplayAlerts = False
Path = "C:\Users\steve\Desktop\" 'change this if you want to save it somewhere else
FileName = Sheets("Summary").range("A4").Value & ".xlsm"
ActiveWorkbook.SaveAs Path & FileName, xlOpenXMLWorkbookMacroEnabled
' Change the format here which matches with the extention above. Choose from the following link
' http://msdn.microsoft.com/en-us/library/office/ff198017.aspx
Application.DisplayAlerts = True
' This saves it in the reports folder as 'Latest Report'
ChDir "C:\Users\steve\Desktop\Reports"
ActiveWorkbook.SaveAs FileName:= _
"C:\Users\steve\Desktop\Reports\Latest Report.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
如果 Sheets("Summary").range("A4").Value
中有一个 /
字符,您需要先替换或删除它,因为该字符不能在文件名中使用。使用以下行来删除该字符。
FileName = Replace(Sheets("Summary").range("A4").Value, "/", "") & ".xlsm"
您也可以将其替换为新的有效字符:
FileName = Replace(Sheets("Summary").range("A4").Value, "/", "-") & ".xlsm"
Replace function VBA Documentation
以下是所有非法字符的列表:
我正在导入每日数据并对其进行处理。我想将文件保存为从A4中提取的数据的日期。
数据最初以 CSV 文件的形式出现,数据格式为 27/06/2020。我在操作过程中重新格式化了数据,但无论如何,当我保存文件时,我得到了
Runtime error 1004 Microsoft Excel cannot access the file "C:\Users\steve\Desktop\D2D75130"
我不清楚是否(以及如何)将其更改为文本格式以便保存 - 但我喜欢将日期保留为日期格式以帮助我进行数据分析的想法。或者也许我应该在日期前添加一个文本字符串,即“销售额(然后是日期)”——如果这样行得通吗?这是我现有的代码:
' This is to save the file on the desktop using the date (content of cell A4) as the filename
' It also saves the file with the name 'Latest Report' in the Reports Folder
Sub FileNameAsCellContent()
Dim FileName As String
Dim Path As String
Application.DisplayAlerts = False
Path = "C:\Users\steve\Desktop\" 'change this if you want to save it somewhere else
FileName = Sheets("Summary").range("A4").Value & ".xlsm"
ActiveWorkbook.SaveAs Path & FileName, xlOpenXMLWorkbookMacroEnabled
' Change the format here which matches with the extention above. Choose from the following link
' http://msdn.microsoft.com/en-us/library/office/ff198017.aspx
Application.DisplayAlerts = True
' This saves it in the reports folder as 'Latest Report'
ChDir "C:\Users\steve\Desktop\Reports"
ActiveWorkbook.SaveAs FileName:= _
"C:\Users\steve\Desktop\Reports\Latest Report.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
如果 Sheets("Summary").range("A4").Value
中有一个 /
字符,您需要先替换或删除它,因为该字符不能在文件名中使用。使用以下行来删除该字符。
FileName = Replace(Sheets("Summary").range("A4").Value, "/", "") & ".xlsm"
您也可以将其替换为新的有效字符:
FileName = Replace(Sheets("Summary").range("A4").Value, "/", "-") & ".xlsm"
Replace function VBA Documentation
以下是所有非法字符的列表: