【问题标题】:VBA - Saving a file - Determine file path based partially on cell valuesVBA - 保存文件 - 部分根据单元格值确定文件路径
【发布时间】: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 建议在上面添加星号将匹配带有部分信息的文件夹。我不应该在该行中使用星号吗?

标签: excel vba filepath


【解决方案1】:

展示基本原理:

编辑:对您的代码进行一些更改...

Sub Tester()

    Dim qNum, fldr
    Dim custName

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

    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 Sub

'get the path for a given q number
Function GetMatchingPath(qNum, custName) As String
    Const ROOT As String = "P:\Weir\" '<< adjust to suit
    Dim f

    f = Dir(ROOT & custName & "\" & qNum & "*", vbDirectory)
    GetMatchingPath = ROOT & custName & "\" & f
End Function

【讨论】:

  • 好的,我尝试实现它,但它似乎没有为 f 保存任何值。查看 Locals 菜单并逐步查看,f 的值从“empty”变为“”。有什么想法吗?
  • 如果f 在调用Dir() 后没有任何值,则没有与您提供的值匹配的文件夹。你能用你试过的确切代码更新你的问题吗?
  • 当然。我要保存文件的文件夹的路径为P:\MyCompany\Syncrude\Q-188888 - 32in Spool\2-Engineering\2.3-Material Take Off。在 B12 我有“Syncrude”,在 B19 我有“Q-188888”。我的代码如下:
  • 在您的问题下,单击“编辑”链接,将您的代码从 VB 编辑器复制并粘贴到问题的底部,选择它,然后使用“{}”按钮对其进行格式化作为代码。
  • 您好 Tim Williams,感谢您提供的所有帮助,但仍然不太正确。当我逐步执行代码时,变量 f 仍然没有存储值,这意味着没有文件夹与该值匹配。我不确定这是怎么回事,因为我正在测试的文件夹路径是 P:\MyCompany\Syncrude\TEST Q-188888。关于为什么 f 不想存储值的任何想法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
相关资源
最近更新 更多