【问题标题】:Excel VBA Creates a Folder, Sub Folders and further sub foldersExcel VBA 创建文件夹、子文件夹和其他子文件夹
【发布时间】:2020-07-18 11:32:28
【问题描述】:

我有一个问题与我在这里看到的其他一些问题非常相似,但他们并没有完全回答我的需要,或者当我尝试过它们时,它导致了一个我不知道如何解决的错误。 只有第 5 级,我无法发表评论以提出问题。

在 excel 中,我有一个文件用于引用文件夹的命名配置文件。

我已尝试将答案用于:Create a folder and sub folder in Excel VBA 并按以下方式对其进行了调整,但当它到达If Functions.FolderExists(path) Then 时会出错

运行时错误“424”:需要对象。

我还需要根据工作表“数据输入”单元格“C44”和“C31”创建文件夹名称,然后我需要向其中添加子文件夹,这些子文件夹在任何单元格中都没有引用,包括: 1. 客户询价 这将有另一个子文件夹,其名称基于“数据输入”单元格“C33”

  1. 设计工程
  2. 图纸
  3. 成本核算
  4. 时间表
  5. 报价

任何帮助将不胜感激。 谢谢,

'requires reference to Microsoft Scripting Runtime
Sub MakeFolder()

Dim strFolder As String, strPath As String

strFolder = CleanName(Range("C31")) ' assumes folder name is in C31
strPath = Range("C44") ' assumes path name is in C44

If Not FolderExists(strPath) Then
'Path doesn't exist, so create full path
    FolderCreate strPath & "\" & strFolder
Else
'Path does exist, but no quote folder
    If Not FolderExists(strPath & "\" & strFolder) Then
        FolderCreate strPath & "\" & strFolder
    End If
End If

End Sub

Function FolderCreate(ByVal path As String) As Boolean

FolderCreate = True
Dim fso As New FileSystemObject

If Functions.FolderExists(path) Then 'This is the part that doesn't work
    Exit Function
Else
    On Error GoTo DeadInTheWater
    fso.CreateFolder path ' could there be any error with this, like if the path is really screwed up?
    Exit Function
End If

DeadInTheWater:
    MsgBox "A folder could not be created for the following path: " & path & ". Check the path name and try again."
    FolderCreate = False
    Exit Function

End Function

Function FolderExists(ByVal path As String) As Boolean

FolderExists = False
Dim fso As New FileSystemObject

If fso.FolderExists(path) Then FolderExists = True

End Function

Function CleanName(strName As String) As String
'will clean part # name so it can be made into valid folder name
'may need to add more lines to get rid of other characters

    CleanName = Replace(strName, "/", "")
    CleanName = Replace(CleanName, "*", "")

End Function

非常感谢任何帮助。谢谢

【问题讨论】:

  • If fso.FolderExists(path) Then
  • @BigBen 哇!那真的很快!非常感谢,只是兴趣,函数和fso有什么区别?显然,其他所有使用它的人都知道要更改它,但对它不熟悉,我不知道要更改它。
  • 我会为您指出FileSystemObject 文档以获得详尽的解释。
  • @BigBen,非常感谢您的帮助!非常感谢,真的太棒了。
  • 如果您将 Functions.FolderExists(path) 更改为 FolderExists(path) 也可以。您无需以Functions 开头。但是,该功能确实不需要,只需使用@BigBen 的解决方案即可。

标签: excel vba


【解决方案1】:

感谢@BigBen、@BrianMStafford 的帮助。我设法想出了这个可行的方法。这会在单元格指定位置的主文件夹中创建 10 个子文件夹。然后它会在文件夹 1 中创建另一个子文件夹。

由于某种原因,我的公司安全性在打开由代码创建的文件时出现问题,其中名称不是来自单元格。所以我计划将所有其他文件夹名称移动到一系列单元格中,希望这能奏效。

之后,当我弄清楚如何操作时,我打算让它打开用户首先使用的文件夹。在我的情况下,这是最后创建的文件夹。希望这可以帮助某人:-)

'requires reference to Microsoft Scripting Runtime
Sub MakeFolder()

Dim strFolder As String, strPath As String

strFolder = CleanName(Range("C31")) ' assumes folder name is in C31
strPath = Range("C44") ' assumes path name is in C44

If Not FolderExists(strPath) Then
'Path doesn't exist, so create full path
    FolderCreate strPath & "\" & strFolder
Else
'Path does exist, but no quote folder
    If Not FolderExists(strPath & "\" & strFolder) Then
        FolderCreate strPath & "\" & strFolder
        FolderCreate strPath & "\" & strFolder & "\" & "01. Customer RFQ"
        FolderCreate strPath & "\" & strFolder & "\" & "02. Design Engineering"
        FolderCreate strPath & "\" & strFolder & "\" & "03. Drawings"
        FolderCreate strPath & "\" & strFolder & "\" & "04. Costings"
        FolderCreate strPath & "\" & strFolder & "\" & "05. Schedules"
        FolderCreate strPath & "\" & strFolder & "\" & "06. Quotation"
        FolderCreate strPath & "\" & strFolder & "\" & "07. Email"
        FolderCreate strPath & "\" & strFolder & "\" & "08. MOMs"
        FolderCreate strPath & "\" & strFolder & "\" & "09. Sales Excellence"
        FolderCreate strPath & "\" & strFolder & "\" & "10. Compliance"
        FolderCreate strPath & "\" & strFolder & "\" & "01. Customer RFQ" & "\" & Range("C33")
    End If
End If

End Sub

Function FolderCreate(ByVal path As String) As Boolean

FolderCreate = True
Dim fso As New FileSystemObject

If fso.FolderExists(path) Then
    Exit Function
Else
    On Error GoTo DeadInTheWater
    fso.CreateFolder path ' could there be any error with this, like if the path is really screwed up?
    Exit Function
End If

DeadInTheWater:
    MsgBox "A folder could not be created for the following path: " & path & ". Check the path name and try again."
    FolderCreate = False
    Exit Function

End Function

Function FolderExists(ByVal path As String) As Boolean

FolderExists = False
Dim fso As New FileSystemObject

If fso.FolderExists(path) Then FolderExists = True

End Function

Function CleanName(strName As String) As String
'will clean part # name so it can be made into valid folder name
'may need to add more lines to get rid of other characters

    CleanName = Replace(strName, "/", "")
    CleanName = Replace(CleanName, "*", "")

End Function

【讨论】:

  • 如果您将文件夹命名为“01...”、“02...”、“10...”,那么它们将在 Windows 资源管理器中以正确的顺序排序,而不是 1 和10 一起排序。