这个SaveAs语法有什么阻止执行?

时间:2015-02-24 05:01:37

标签: excel vba excel-vba

在本网站上开发了具有确切示例的代码,但不会在SaveAs...行运行和停止。谁能发现我的错误?

我收到此错误:

enter image description here

然而路径C:\dads\Downloads\确实存在,该文件未被其他程序使用,并且它具有不同的名称。我错过了什么?

Option Explicit

Sub PickupRoutes_Click()

    Dim Rng As Range
    Dim Lrow As Integer, NewFile As String

    Lrow = 7
    Set Rng = ActiveSheet.Range(Cells(2, 33), Cells(Lrow, 38))

    '~~> Copy the range from the current Workbook
    Rng.Select
    Rng.Copy

    'Establish a new Workbook and paste range
    Application.Workbooks.Add
    ActiveWorkbook.ActiveSheet.Range("A1").Select

    '~~> Paste rng in Cell A1.
    ActiveWorkbook.ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues, _
        Operation:=xlNone, SkipBlanks:=False, Transpose:=False

    '~~>. Save the file
    NewFile = "C:\dads\Downloads\" & "PlanMyRoute_" & _
        Format(Date, "mm-dd-yyyy") & ".xlsm"
    ActiveWorkbook.SaveAs NewFile, _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.Close SaveChanges:=False
    MsgBox "PlanMyRoute file has been saved "

End Sub

1 个答案:

答案 0 :(得分:0)

如果文件路径不存在,则错误消息将显示内存地址,这就是错误消息和代码不匹配的原因。

您似乎缺少路径中的Users目录。

更改为:

NewFile = "C:\Users\dads\Downloads\" & "PlanMyRoute_" & _
    Format(Date, "mm-dd-yyyy") & ".xlsm"