保存为日期和时间不起作用

时间:2015-08-11 18:32:36

标签: excel vba excel-vba

它应该保存为文件名:文件夹\测试位置'单元格C27中的内容'然后是数据和时间。我得到:'008 11 2015 00 00 00'。如何使用“/”和“:”清除它?注意第一个0就是我使用的测试编号。

此宏也在测试软件使用的模板中,这就是为什么它必须使用Auto_open,但另一个问题是当它作为非模板文件保存时,打开时尝试在非模板中运行宏文件。如何在保存为文件/非模板文件时将宏保存或禁用?

Sub Auto_Open()

Dim FileName    As String
Dim FilePath    As String
Dim FileDate    As String

MyNote = "Is Cell 'C27' Overview Information" & SavePath & " Location_1,2,3,or 4?"

Answer = MsgBox(MyNote, vbQuestion + vbYesNo)

If Answer = vbYes Then

    FilePath = "C:\Users\aholiday\Desktop\FRF_Data_Macro_Insert_Test"
    FileName = Sheets("Data").Range("C27").Text
    ThisWorkbook.SaveAs FileName:=FilePath & "\" & FileName

    Dim FileCopyName    As String
    Dim FileCopyPath    As String

    FilePath = "C:\Users\aholiday\Desktop\Backup"
    FileName = Sheets("Data").Range("C27").Text
    FileDate = Format(Date, "mm dd yyyy hh mm ss")
    ThisWorkbook.SaveAs FileName:=FilePath & "\" & FileName & FileDate

    MsgBox "File was saved!"

    MyNote = "Open FRF Data Sheet?(After Forth Test Only)"

    Answer = MsgBox(MyNote, vbQuestion + vbYesNo)

    If Answer = vbYes Then

        Workbooks.Open ("FRF_Data_Sheet_Template.xlsm")

    Else
        MsgBox "Ready for Next Test, Please Exit."
    End If

Else
    MsgBox "File was not saved, Please Use Location_1,2,3or,4 Durring SIG ATM Test"
End If

End Sub

解决:

  Sub Auto_Open()

  With Range("A30")
  .Value = Time
  .NumberFormat = "h-mm-ss AM/PM"
  End With


    Dim FileName    As String
    Dim FilePath    As String
    Dim FileDate    As String

    MyNote = "Is Cell 'B27' Overview Information" & SavePath & " Location1,2,3,or 4?"

    Answer = MsgBox(MyNote, vbQuestion + vbYesNo)

    If Answer = vbYes Then

        FilePath = "C:\Users\aholiday\Desktop\FRF_Data_Macro_Insert_Test"
        FileName = Sheets("Data").Range("C27").Text
        ThisWorkbook.SaveAs FileName:=FilePath & "\" & FileName

        Dim FileCopyName    As String
        Dim FileCopyPath    As String
        Dim FileTime        As String

        FilePath = "C:\Users\aholiday\Desktop\Backup"
        FileName = Sheets("Data").Range("B27").Text
        FileTime = Sheets("Data").Range("A30").Text


        ThisWorkbook.SaveAs FileName:=FilePath & "\" & FileName & FileTime & ".xlsx", FileFormat:=xlOpenXMLWorkbook

        MsgBox "File was saved!"
        MsgBox "Ready for Next Test, Please Exit."


    Else
    MsgBox "File was not saved, Please Use Location_1,2,3or,4 Durring SIG ATM Test"
    End If

 End Sub

1 个答案:

答案 0 :(得分:1)

你不能在文件名中有\。

对于日期部分,请使用格式化功能。如果需要,您可以使用" MM-dd-yyy"

来定义日期格式
ThisWorkbook.SaveAs FileName:=FilePath & "\" & FileName & Format(FileDate, "MM-dd-yyyy") & ".xlsx", FileFormat:=xlOpenXMLWorkbook

使用FileFormat:= xlOpenXMLWorkbook将其另存为没有宏的工作簿。