【发布时间】: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