英文:
Filename based on value cell,but with specific format
问题
Sub CopyToCSV()
Dim MyPath As String
Dim MyFileName As String
'指定路径和文件名:
MyPath = "C:\Hello"
MyFileName = "Greetings_" & Format(Date, "yyyymmdd")
'确保路径以""结束:
If Not Right(MyPath, 1) = "" Then MyPath = MyPath & ""
'确保文件名以".csv" 结尾:
If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
'复制工作表到新工作簿:
Sheets("final step").Copy
'新工作簿成为活动工作簿:
With ActiveWorkbook
'将新工作簿保存到指定文件夹/文件名:
.SaveAs Filename:= _
MyPath & MyFileName, _
FileFormat:=xlCSV, _
CreateBackup:=False, _
Local:=True
'关闭文件
.Close False
End With
End Sub
英文:
i want to save a filename based on a cell value (AA2 for example) which has date format (dd/mm/yyyy) but i want it to be like yyyymmdd without slash (/). This code works fine but gives me the current date (which doesnt help)
Sub CopyToCSV()
Dim MyPath As String
Dim MyFileName As String
'The path and file names:
MyPath = "C:\Hello"
MyFileName = "Greetings_" & Format(Date, "yyyymmdd")
'Makes sure the path name ends with "\":
If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
'Makes sure the filename ends with ".csv"
If Not Right(MyFileName, 4) = ".csv" Then Hello = Hello & ".csv"
'Copies the sheet to a new workbook:
Sheets("final step").Copy
'The new workbook becomes Activeworkbook:
With ActiveWorkbook
'Saves the new workbook to given folder / filename:
.SaveAs Filename:= _
MyPath & MyFileName, _
FileFormat:=xlCSV, _
CreateBackup:=False, _
Local:=True
'Closes the file
.Close False
End With
End Sub
答案1
得分: 0
像这样:
Sub CopyToCSV()
Const MY_PATH As String = "C:\Hello\"
Dim MyFileName As String, dt As Date
dt = ThisWorkbook.Sheets("info").Range("AA2").Value '<< specify sheet name
MyFileName = MY_PATH & "Greetings_" & Format(dt, "yyyymmdd") & ".csv"
ThisWorkbook.Sheets("final step").Copy
With ActiveWorkbook 'The new workbook becomes Activeworkbook
.SaveAs Filename:=MyFileName, FileFormat:=xlCSV, _
CreateBackup:=False, Local:=True
.Close False
End With
End Sub
英文:
Like this:
Sub CopyToCSV()
Const MY_PATH As String = "C:\Hello\"
Dim MyFileName As String, dt As Date
dt = ThisWorkbook.Sheets("info").Range("AA2").Value '<< specify sheet name
MyFileName = MY_PATH & "Greetings_" & Format(dt, "yyyymmdd") & ".csv"
ThisWorkbook.Sheets("final step").Copy
With ActiveWorkbook 'The new workbook becomes Activeworkbook
.SaveAs Filename:=MyFileName, FileFormat:=xlCSV, _
CreateBackup:=False, Local:=True
.Close False
End With
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论