【问题标题】:VBA code to check and create folder system and save file用于检查和创建文件夹系统并保存文件的 VBA 代码
【发布时间】:2020-02-26 15:42:44
【问题描述】:

我正在寻找一个代码,该代码采用一个活动工作表,一旦完成并选择一个按钮,它将根据多个单元格值将其保存为文件夹/子文件夹系统中的新工作簿。一些单元格可能保持不变,但其他单元格可能会发生变化,从而提供可能已经部分存在或根本不存在的各种潜在路径。

我已经设法将一个代码放在一起,但是当我更改其中一个单元格值时,最终会稍微改变路径,我收到以下错误:运行时错误 75:路径/文件访问错误.

我假设它与一些已经存在的文件夹和子文件夹有关。不确定。

Sub Check_CreateFolders_YEAR_SO_WODRAFT()

    Dim wb As Workbook
    Dim Path1 As String
    Dim Path2 As String
    Dim Path3 As String
    Dim Path4 As String
    Dim myfilename As String
    Dim fpathname As String

    Set wb = Workbooks.Add
    ThisWorkbook.Sheets("Jobs Sheet").Copy Before:=wb.Sheets(1)
    Path1 = "C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board"
    Path2 = Range("A23")
    Path3 = Range("I3")
    Path4 = Range("I4")
    myfilename = Range("I3").Value & Range("A1").Value & Range("I4").Value & Range("A1").Value & Range("AA1").Value
    fpathname = Path1 & "\" & Path2 & "\" & Path3 & "\" & Path4 & "\" & myfilename & ".xlsx"

    If Dir("C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 & "\" & Path3 & "\" & Path4, vbDirectory) = "" Then
        MkDir Path:="C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2
        MkDir Path:="C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 & "\" & Path3
        MkDir Path:="C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 & "\" & Path3 & "\" & Path4
        MsgBox "Completed"
    Else
        MsgBox "Sales Order Folder Already Exists so we'll save it in there"
    End If

    MsgBox "You are trying to save the file to:" & vbCrLf & fpathname
    wb.SaveAs filename:=fpathname & ".xlsx"

End Sub

理想的结果是基于单元格值创建文件夹系统。如前所述,部分路径可能已经存在,但代码需要确定路径是否更改以及更改位置,然后创建正确的路径,然后保存新文件。

【问题讨论】:

    标签: excel vba directory path save-as


    【解决方案1】:

    使用下面的API function 创建目录,那么如果路径已经部分存在或根本不存在,您就不必担心。

    Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" ( _
      ByVal lpPath As String) As Long
    

    你会这样调用函数

    MakeSureDirectoryPathExists "C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2
    

    只要确保Path2\ 结尾,因为

    如果路径的最后组成部分是目录,而不是文件名,则字符串必须以反斜杠字符结尾。

    更新:这应该是API函数的代码

    Option Explicit
    
    Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" ( _
      ByVal lpPath As String) As Long
    
    Sub Check_CreateFolders_YEAR_SO_WODRAFT()
    
        Dim wb As Workbook
        Dim Path1 As String
        Dim Path2 As String
        Dim Path3 As String
        Dim Path4 As String
        Dim myfilename As String
        Dim fpathname As String
    
        Set wb = Workbooks.Add
        ThisWorkbook.Sheets("Jobs Sheet").Copy Before:=wb.Sheets(1)
        Path1 = "C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board"
        Path2 = Range("A23")
        Path3 = Range("I3")
        Path4 = Range("I4")
        myfilename = Range("I3").Value & Range("A1").Value & Range("I4").Value & Range("A1").Value & Range("AA1").Value
        fpathname = Path1 & "\" & Path2 & "\" & Path3 & "\" & Path4 & "\" & myfilename & ".xlsx"
    
        If Dir("C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 & "\" & Path3 & "\" & Path4, vbDirectory) = "" Then
            MakeSureDirectoryPathExists "C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 & "\" & Path3 & "\" & Path4 & "\"
            ' MkDir Path:="C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 
            ' MkDir Path:="C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 & "\" & Path3
            ' MkDir Path:="C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 & "\" & Path3 & "\" & Path4
            MsgBox "Completed"
        Else
            MsgBox "Sales Order Folder Already Exists so we'll save it in there"
        End If
    
        MsgBox "You are trying to save the file to:" & vbCrLf & fpathname
        wb.SaveAs Filename:=fpathname & ".xlsx"
    
    End Sub
    

    【讨论】:

    • 嗨 Storax,我是这方面的新手,我说的是:
    • MakeSureDirectoryPathExists "C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2
    • …..在一个位置还是在每一行?
    • 嗨 Storax,我已经修改了以下内容,但不满意:
    • MakeSureDirectoryPathExists "C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 \ " & Path2 & " \ "" MakeSureDirectoryPathExists "C:\Users\jackson. wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 & "\" & Path3 MakeSureDirectoryPathExists "C:\Users\jackson.wills\Sparter Ltd\Engineer Order - e-Board\" & Path2 & "\" & Path3 & "\" & Path4
    猜你喜欢
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多