Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 保存为PDF时更改日期格式_Excel_Vba_Date - Fatal编程技术网

Excel 保存为PDF时更改日期格式

Excel 保存为PDF时更改日期格式,excel,vba,date,Excel,Vba,Date,我面临以下问题: 我想使用VBA将工作表保存为PDF。文件名应该包括一些单元格的值,包括一个保存日期的单元格。此日期已更改为格式“dd-mm-yyyy”(通过代码或“主页”选项卡中的数字格式下拉列表),因为文件名不能包含“/”,但当我查看公式栏时,日期仍然是“dd/mm/yyyy” 运行代码时,文件名以该格式返回日期。。。 如何更改日期格式,以便正确地将其包含在文件名中 Dim wsA As Worksheet Dim wbA As Workbook Dim strDate As Range

我面临以下问题:
我想使用VBA将工作表保存为PDF。文件名应该包括一些单元格的值,包括一个保存日期的单元格。此日期已更改为格式“dd-mm-yyyy”(通过代码或“主页”选项卡中的数字格式下拉列表),因为文件名不能包含“/”,但当我查看公式栏时,日期仍然是“dd/mm/yyyy”

运行代码时,文件名以该格式返回日期。。。 如何更改日期格式,以便正确地将其包含在文件名中


Dim wsA As Worksheet
Dim wbA As Workbook
Dim strDate As Range
Dim strName As Range
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveWorkbook.Sheets("Devis")
Set strName = ActiveWorkbook.Sheets("Calc").Range("Designation")
Set strDate = ActiveWorkbook.Sheets("Calc").Range("arrival")

strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
strDate = Replace(strDate, "/", "-")

strFile = strName & "_" & strDate & ".pdf"
strPathFile = strPath & strFile

myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strPathFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

If myFile <> "False" Then
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
    'confirmation message with file info
    MsgBox "PDF file has been created: " _
      & vbCrLf _
      & myFile
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Could not create PDF file"
    Resume exitHandler


End Sub


将wsA设置为工作表
将wbA设置为工作簿
暗标准值范围
暗淡的strName作为范围
将strPath设置为字符串
作为字符串的Dim strFile
将strPathFile设置为字符串
Dim myFile作为变量
关于错误转到错误处理程序
设置wbA=ActiveWorkbook
设置wsA=ActiveWorkbook.Sheets(“设计”)
设置strName=ActiveWorkbook.Sheets(“计算”).Range(“名称”)
Set strDate=ActiveWorkbook.Sheets(“计算”).Range(“到达”)
strPath=wbA.Path
如果strPath=“”,则
strPath=Application.DefaultFilePath
如果结束
strPath=strPath&“\”
标准日期=替换(标准日期,“/”,“-”)
strFile=strName&“&”strDate&“.pdf”
strPathFile=strPath&strFile
myFile=Application.GetSaveAsFilename_
(InitialFileName:=strPathFile_
FileFilter:=“PDF文件(*.PDF),*.PDF”_
标题:=“选择要保存的文件夹和文件名”)
如果myFile为“False”,则
wsA.ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=myFile_
质量:=xlQualityStandard_
IncludeDocProperties:=True_
IgnorePrintAreas:=假_
OpenAfterPublish:=False
'带有文件信息的确认消息
MsgBox“已创建PDF文件:”_
&vbCrLf_
&我的文件
如果结束
出口商:
出口接头
错误处理程序:
MsgBox“无法创建PDF文件”
复出机
端接头
您将看到我试图在代码中用“-”替换“/”但这似乎没有帮助。。。
期待您的建议

当您使用
strDate=Replace(strDate,“/”,“-”
时,您正在调用单元格中的值。即使在上面看到日期,Excel上的日期实际上也是数字

所以
Replace(strDate,“/”,“-”
不起作用,因为没有什么可替换的

使用
格式
可以更改值的显示方式,并且可以将新格式化的值保存到字符串变量中


这就解释了为什么strFile=strName&“”和Format(strDate,“dd-mm-yyyy”)和.pdf“可以工作。

试试
strFile=strName&“”和Format(strDate,“dd-mm-yyyy”)和.pdf“
工作得很好!虽然很简单。。。非常感谢@麦克风没问题。如果这个答案对你有用,pelase,把它标记为正确,这样它就可以关闭了