【发布时间】:2019-01-02 14:46:59
【问题描述】:
一切正常,直到我到达 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
` 它正确地找到了文件路径,看起来好像应该在正确的位置完成保存,但我总是收到 1004 错误。有什么想法吗?
【问题讨论】:
-
Dir("C:\Folders\Q-180575*")(例如)应该可以工作,假设没有名为(例如)“Q-1805759...”的文件夹 -
是的,有办法。从Reading values from a Cell 开始,然后查看String Manipulation,然后查看SaveAs method。如果您遇到任何错误,请使用您尝试过的代码返回并发布它们。我们都在等待,祝你好运:)
-
感谢您的回复。这是我迄今为止的尝试,但它不起作用。 Public Sub SaveWorkbook() Dim custName As String Dim quoteNum As String Dim myFilename As String Dim myFilePath As String custName = Range("B12") quoteNum = Range("B19") myFilePath = "P:\MyCompany\custName\quoteNum*\ 2-Engineering\2.3 - Material Take Off\" myFilename = quoteNum & "- " & custName ActiveWorkbook.SaveAs filename:=myFilePath & myFilename & ".xlsm", FileFormat:=xlNormal End Sub
-
你的路径
myFilePath = "P:\MyCompany\custName\quoteNum*\2-Engineering\2.3 - Material Take Off\"中有一个星号,它可能对此不太满意,但我用 C:\Temp\ 对其进行了测试,它运行良好。 -
Tim Williams 建议在上面添加星号将匹配带有部分信息的文件夹。我不应该在该行中使用星号吗?