我有一个问题与我在这里看到的其他一些问题非常相似,但它们并没有完全回答我需要的问题,或者当我尝试它们时,它导致了一个我不知道如何解决的错误。
只有5级我无法发表评论来提问。
在 Excel 中,我有一个用于引用文件夹的命名配置文件的文件。
我尝试将答案用于:在Excel VBA中创建文件夹和子文件夹 https://stackoverflow.com/questions/10803834/create-a-folder-and-sub-folder-in-excel-vba并按照下面的方式对其进行了调整,但是当它到达时会出错If Functions.FolderExists(path) Then
it says
运行时错误“424”:需要对象。
我还需要根据工作表“数据输入”单元格“C44”和“C31”创建文件夹名称,然后我需要向其中添加子文件夹,这些子文件夹在任何单元格中都没有引用,包括:
1. 客户询价
这将有一个进一步的子文件夹,其名称基于“数据输入”单元格“C33”
- 设计工程
- Drawings
- Costings
- 时间表
- 引述
任何帮助将不胜感激。
谢谢你,
'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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)