【发布时间】: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”
- 设计工程
- 图纸
- 成本核算
- 时间表
- 报价
任何帮助将不胜感激。 谢谢,
'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 的解决方案即可。