保存NewWorkbook时运行时错误

时间:2016-06-26 21:06:00

标签: excel-vba vba excel

所以我尝试了一些放置问题,我似乎无法弄清楚为什么这在我的一些宏中起作用而不是我当前的一些?有什么指针吗?

Private Sub CommandButton1_Click()

Dim filelocation1 As String
Dim wbO As Workbook
Dim wsO as Worksheet


filelocation1 = "C:\Users\Ashleysaurus\Desktop\doug" & "\" & Now() & ".xls"

Set wbO = Workbooks.Add

With wbO
    Set wsO = wbO.Sheets("Sheet1")
    wbO.SaveAs Filename:=filelocation1, FileFormat:=56

1 个答案:

答案 0 :(得分:1)

您不能在文件名中使用/

  

“C:\ Users \ Ashleysaurus \ Desktop \ doug”& “\”&现在()&是 “.xls”

     

返回:“C:\ Users \ Ashleysaurus \ Desktop \ doug \ 6/26/2016 11:15:54 PM.xls”

使用:

  

“C:\ Users \ Ashleysaurus \ Desktop \ doug”& “\”&格式(现在,“yyyy-mm-dd hh-mm”)& “.xls”

     

返回:“C:\ Users \ Ashleysaurus \ Desktop \ doug \ 2016-06-26 23-15.xls”

Private Sub CommandButton1_Click()

    Dim filelocation1 As String
    Dim wbO As Workbook
    Dim wsO As Worksheet


    filelocation1 = "C:\Users\Ashleysaurus\Desktop\doug" & "\" & Format(Now, "yyyy-mm-dd hh-mm") & ".xls"

    Set wbO = Workbooks.Add

    With wbO
        Set wsO = wbO.Sheets("Sheet1")
        wbO.SaveAs Filename:=filelocation1, FileFormat:=56

    End With
End Sub