根据数值单元格确定的文件名,但具有特定格式。

huangapple go评论57阅读模式
英文:

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 = &quot;C:\Hello\&quot;
    Dim MyFileName As String, dt As Date
    
    dt = ThisWorkbook.Sheets(&quot;info&quot;).Range(&quot;AA2&quot;).Value &#39;&lt;&lt; specify sheet name
    MyFileName = MY_PATH &amp; &quot;Greetings_&quot; &amp; Format(dt, &quot;yyyymmdd&quot;) &amp; &quot;.csv&quot;
    
    ThisWorkbook.Sheets(&quot;final step&quot;).Copy
    
    With ActiveWorkbook &#39;The new workbook becomes Activeworkbook
        .SaveAs Filename:=MyFileName, FileFormat:=xlCSV, _
            CreateBackup:=False, Local:=True
        .Close False
    End With
End Sub

huangapple
  • 本文由 发表于 2023年2月24日 00:00:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定