VBA:用于创建子文件夹和文件夹的代码不起作用

时间:2018-09-12 13:47:38

标签: vba

我从在线来源获得了此代码,无法弄清楚我必须更改什么,为了使其正常工作,没有在所需位置创建文件夹,并且我不知道在哪里创建文件夹?

在excel中,我有3个命令按钮,一个用于将文件保存到指定位置,一个用于通过电子邮件将其发送给同事,最后是我遇到问题的命令按钮。病包括以下图片。

并且有可能引起代码之间的连锁反应,一个代码一旦完成,另一个代码就会开始,因为我可以让它们在自己的atm上正常工作。

enter image description here

1 个答案:

答案 0 :(得分:1)

Mkdir只能创建一个目录。您正在尝试通过提供“ 9999 William Cox ltd \ BRAKEL”来赚两个。

首先创建“ 9999 William Cox ltd”,然后创建其子目录。

这里有一些代码将通过循环生成所有子目录:

将以下功能添加到您的代码模块中:

git rev-list --all | xargs -I% git --no-pager grep -F -e str % -- fileA

然后替换为:

Private Function makeDir(parentDir As String, childDir As String) As String
    'Checks if supplied directory name exists in current path, if not then create.
    childDir = parentDir & _
        IIf(Left(childDir, 1) = "\", "", "\") & _
        childDir & _
        IIf(Right(childDir, 1) = "\", "", "\")

    On Error Resume Next
    MkDir childDir
    On Error GoTo 0

    makeDir = childDir
End Function

Public Sub makePath(parentDir As String, childPath As String)
    Dim i As Integer
    Dim subDirs As Variant
    Dim newdir As String
    Dim fPath As String

    fPath = parentDir
    subDirs = Split(childPath, "\")

    For i = 0 To UBound(subDirs)

        newdir = subDirs(i)
        fPath = makeDir(fPath, newdir)

    Next i

End Sub

与此:

MkDir ("T:\Estimating\William Cox Project Enquiries 2018\" & fPath)
If Err.Number <> 0 Then
    Err.Clear
End If

您还应该删除makePath "T:\Estimating\William Cox Project Enquiries 2018\", fpath ,以便可以捕获其他任何错误-在您的路径中(根据屏幕快照)可能还有另一个错误,该错误的开头两次出现“ T:\ Estimating”。

相关问题