【问题标题】:Run-time error '1004': Method SaveAs of object _Workbook failed运行时错误“1004”:对象 _Workbook 的方法 SaveAs 失败
【发布时间】:2019-01-05 01:39:30
【问题描述】:

我正在编写一个程序,该程序根据工作簿中的某些单元格值将工作簿保存到特定文件夹。一切正常,直到我到达 ActiveWorkbook.SaveAs 行,在那里我得到一个运行时错误 1004。

`Sub Tester()

Dim qNum, fldr As String
Dim custName As String
Dim myFileName As String
Dim completePath As String
Dim division As String

custName = Range("B12").Value
qNum = Range("B19").Value

If custName = "CNUL - Albian" Then
    custName = "CNRL"
    division = "Albian"
End If
If custName = "CNUL - Horizon" Then
    custName = "CNRL"
    division = "Horizon"
End If
If custName = "CNRL - Albian" Then
    custName = "CNRL"
    division = "Albian"
End If
If custName = "CNRL - Horizon" Then
    custName = "CNRL"
    division = "Horizon"
End If

If custName = "CNRL" Then
    fldr = GetMatchingPathCNRL(qNum, custName, division) '<< find the        matching folder
    If Len(fldr) > 0 Then
        Debug.Print "Found folder for customer=" & custName & _
                        ", Qnum=" & qNum & vbLf & fldr
            '...use this path

    Else
        MsgBox "No matching folder!", vbExclamation
    End If
Else
    fldr = GetMatchingPath(qNum, custName) '<< find the matching folder
    If Len(fldr) > 0 Then
        Debug.Print "Found folder for customer=" & custName & _
                    ", Qnum=" & qNum & vbLf & fldr
        '...use this path

    Else
        MsgBox "No matching folder!", vbExclamation
    End If
End If


myFileName = custName & " " & qNum & " " & "MTO Rev A"
completePath = fldr & "\" & myFileName

ActiveWorkbook.SaveAs Filename:=completePath
End Sub

Function GetMatchingPath(qNum, custName) As String
Const ROOT As String = "P:\MyCompany\" '<< adjust to suit
Dim f
f = Dir(ROOT & custName & "\*" & qNum & "*", vbDirectory)
GetMatchingPath = ROOT & custName & "\" & f
End Function


Function GetMatchingPathCNRL(qNum, custName, division) As String
Const ROOT As String = "P:\MyCompany\" '<< adjust to suit
Dim f
f = Dir(ROOT & custName & "\" & division & "\*" & qNum & "*", vbDirectory)
GetMatchingPathCNRL = ROOT & custName & "\" & f
End Function

` 这个想法是,如果客户是 CNRL,则需要通过一层额外的文件夹。开头的 4 个“if”语句只是为了引导人们在电子表格中输入信息的几种不同方式。

当它到达保存文件行时,我总是收到 1004 错误,但变量都存储了正确的名称和文件夹路径。有什么想法吗?

【问题讨论】:

  • debug.print completePath 返回即时窗口是什么?
  • @jeffreyweir - 为客户找到文件夹=SYNCRUDE,Qnum=Q-188888 P:\MyCompany\SYNCRUDE\
  • 这不是我问的。在 SaveAs 之前将debug.print completePath 添加到您的代码中,并让我们知道它返回的内容。

标签: excel vba compiler-errors save


【解决方案1】:

GetMatchingPathCNRL 的返回路径中没有 division

f = Dir(ROOT & custName & "\" & division & "\*" & qNum & "*", vbDirectory)
GetMatchingPathCNRL = ROOT & custName & "\" & f

应该是

GetMatchingPathCNRL = ROOT & custName & "\" & division & "\" & f

编辑:我认为最好将“路径”逻辑集中到一个地方,而不是有两个不同的函数,你必须弄清楚要调用哪一个......

Function GetMatchingPath(qNum, custName, division) As String
    Const ROOT As String = "P:\MyCompany\" '<< adjust to suit
    Dim fPath As String, f

    fPath = ROOT & custName & "\"
    If custName = "CNRL" Then fPath = fPath & division & "\"

    f = Dir(fPath & "*" & qNum & "*", vbDirectory)
    GetMatchingPath = IIf(f <> "", fPath & f, "")
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    相关资源
    最近更新 更多