Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如果在vba宏中找到“复制到另一个文件夹”,请使用文件名在文件夹和子文件夹中搜索文件_Excel_Vba - Fatal编程技术网

Excel 如果在vba宏中找到“复制到另一个文件夹”,请使用文件名在文件夹和子文件夹中搜索文件

Excel 如果在vba宏中找到“复制到另一个文件夹”,请使用文件名在文件夹和子文件夹中搜索文件,excel,vba,Excel,Vba,如果在vba宏中找到“复制到另一个文件夹”,请使用文件名在文件夹和子文件夹中搜索文件 代码显示没有错误,但文件不是从文件夹中复制的,我需要循环子文件夹并找到一个文件 Sub copy_files_from_subfolders() Dim fso As Object Dim fld As Object Dim fsofile As Object Dim fsofol As Object sourcepath = "FINAL CUT\"

如果在vba宏中找到“复制到另一个文件夹”,请使用文件名在文件夹和子文件夹中搜索文件 代码显示没有错误,但文件不是从文件夹中复制的,我需要循环子文件夹并找到一个文件

Sub copy_files_from_subfolders()    
    Dim fso As Object
    Dim fld As Object
    Dim fsofile As Object
    Dim fsofol As Object

    sourcepath = "FINAL CUT\"
    destinationpath = "Desa\MECA\"

    If Right(sourcepath, 1) <> "\" Then
    sourcepath = sourcepath & "\"
    End If

    Set fso = CreateObject("scripting.filesystemobject")
    Set fld = fso.GetFolder(sourcepath)
    If fso.FolderExists(fld) Then
        For Each fsofol In fso.GetFolder(sourcepath).SubFolders
            For Each fsofile In fsofol.Files
                If Right(fsofile, 6) = 566978 Then
                fsofile.Copy destinationpath
            End If
            Next
        Next
    End If
End Sub
Sub-copy_files_from_subfolders()
作为对象的Dim fso
作为对象的Dim fld
作为对象的Dim fsofile
作为对象的Dim fsofol
sourcepath=“最终切割”
destinationpath=“Desa\MECA\”
如果正确(sourcepath,1)“\”则
sourcepath=sourcepath&“\”
如果结束
设置fso=CreateObject(“scripting.filesystemobject”)
Set fld=fso.GetFolder(sourcepath)
如果存在fso.Folder(fld),则
对于fso.GetFolder(sourcepath).子文件夹中的每个fsofol
对于fsofol.Files中的每个fsofile
如果正确(fsofile,6)=566978,则
fsofile.Copy destinationpath
如果结束
下一个
下一个
如果结束
端接头

您正在使用正确的函数搜索号码,但这并没有考虑它后面的文件扩展名。您可以尝试以下操作(假设扩展名相同):

如果文件扩展名的长度不同,可以确定点在名称中的位置,并使用Mid函数

或者,您可以使用以下函数检查您要查找的数字是否出现在文件名中,而不是使用正确的函数:

If InStr(1, fsofile, "566978") <> 0 then
如果InStr(1,fsofile,“566978”)为0,则

只有当文件的数字字符串较长时,才会出现问题,因为例如,您可能有一个名为“123556978123.pdf”的文件,这将是一个误报。

以下是我找到的答案
从子文件夹中复制子文件() 作为对象的Dim fso 作为对象的Dim fld 作为对象的Dim fsofile 作为对象的Dim fsofol

    sourcepath = "FINAL CUT\"
    destinationpath = "Desa\MECA\"

    If Right(sourcepath, 1) <> "\" Then
    sourcepath = sourcepath & "\"
    End If

    Set fso = CreateObject("scripting.filesystemobject")
    Set fld = fso.GetFolder(sourcepath)
    If fso.FolderExists(fld) Then
        For Each fsofol In fso.GetFolder(sourcepath).SubFolders
            For Each fsofile In fsofol.Files
                If InStr(1, fsofile.Name, 566978 & "_PTA") = 1 Then
            fsofile.Copy destinationpath
            End If
            Next
        Next
    End If
End Sub
sourcepath=“最终切割”
destinationpath=“Desa\MECA\”
如果正确(sourcepath,1)“\”则
sourcepath=sourcepath&“\”
如果结束
设置fso=CreateObject(“scripting.filesystemobject”)
Set fld=fso.GetFolder(sourcepath)
如果存在fso.Folder(fld),则
对于fso.GetFolder(sourcepath).子文件夹中的每个fsofol
对于fsofol.Files中的每个fsofile
如果InStr(1,fsofile.Name,566978&“PTA”)=1,则
fsofile.Copy destinationpath
如果结束
下一个
下一个
如果结束
端接头

目标路径中的所有文件夹都存在吗?很抱歉,您要求的是目标路径,目标路径中只有一个文件夹。您确定要检查的文件名以数字566978结尾吗?文件夹和子文件夹中大约有许多文件,我需要找到一些以数字6开头的pdf文件,然后是_PTA,例如123456_PTA.pdf、123456_AOP.pdf、123456_HUD27011.pdf,我需要指定这些文件,但每次搜索的数量可能会有所不同。但是数字后面的名字是我上面提到的,这就是我按数字搜索的原因。大约有很多文件,但我只需要3个文件,以6位数字开头,例如123456_PTA.pdf、123456_AOP.pdf、123456_HUD27011.pdf,但有很多文件都有这个数字,但我只需要将这些文件复制到目标文件夹中。感谢您的帮助文件夹和子文件夹中有很多文件,我需要查找一些pdf文件,以6位数字开头,然后是_PTA,例如123456_PTA.pdf、123456_AOP.pdf、123456_HUD27011.pdf,我需要指定这些文件,但每次搜索的数字可能会有所不同。但是数字后面的名字是我上面提到的,这就是我用数字搜索pdf文件的原因numbers@DESA如果您正在查找的文件名中始终最多有6位数字,则可以使用:If InStr(1,fsofile,“566978”)-1Then@DESA我编辑了我的原始答案,加入了InStr选项。看一看。我编辑了这行,但它将子文件夹中所有可用的文件复制到destination,但问题是它只检查子文件夹的级别,有人能告诉我如何在所有子文件夹中搜索,例如mainfolder->subfolder->subfolder这样的子文件夹。
    sourcepath = "FINAL CUT\"
    destinationpath = "Desa\MECA\"

    If Right(sourcepath, 1) <> "\" Then
    sourcepath = sourcepath & "\"
    End If

    Set fso = CreateObject("scripting.filesystemobject")
    Set fld = fso.GetFolder(sourcepath)
    If fso.FolderExists(fld) Then
        For Each fsofol In fso.GetFolder(sourcepath).SubFolders
            For Each fsofile In fsofol.Files
                If InStr(1, fsofile.Name, 566978 & "_PTA") = 1 Then
            fsofile.Copy destinationpath
            End If
            Next
        Next
    End If
End Sub