Excel VBA创建一个文件夹,子文件夹以及其他子文件夹

史蒂文·伯恩

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

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

我曾尝试使用了答案:在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

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

史蒂文·伯恩

感谢@ 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Excel VBA中创建文件夹和子文件夹

在 Python 中浏览和合并文件夹及其子文件夹中的 Excel 文件

从文件夹和子文件夹中获取文件列表 Excel VBA

Excel vba 下一个发票编号,按月创建自动目录文件夹

从 excel 表创建文件夹和子文件夹和子文本文件

循环浏览包含子文件夹的文件夹以查找excel文件

Excel VBA 搜索子文件夹

读取子文件夹中的 Excel 文件以及如何使用子文件夹名称作为新列值?

将文件夹中的 Excel 文件复制到一个 Excel 文件中

在VBA Excel中动态创建文件夹

使用excel VBA创建多个文件夹

从不同的子文件夹访问VBA导入特定的Excel文件

使用表作为工作表为文件夹中的每个Access文件(.mdb)创建一个Excel文件

如何在文件夹中的所有(关闭)Excel文件上运行一个VBA宏?

使用 Excel 2016 获取去年上月文件夹中的最后一个文件

将Excel文件工作簿合并到一个文件夹中

将每个 Excel 文件分别压缩到一个文件夹中。

删除文件夹VBA中的非Excel文件

删除文件,然后在Excel 2010 VBA中的文件夹

使用VBA获取文件夹中的Excel文件列表

Excel VBA 在文件夹中找不到 xlsx 文件

Excel VBA从文件夹中的文件更新主列表

Excel VBA忽略目录中的特定文件夹/文件

从excel文件vba循环文件夹名称

VBA excel 计算文件夹(和文件)的总数

在文件夹中搜索文件夹 (Excel-VBA)

Excel VBA:文件夹名称未知但扩展名已知的文件夹路径

我如何使用VBA在Excel工作表中创建例如5个文件夹?

VBA Excel将文件保存在Excel宏创建的文件夹中