循环遍历文件夹 vba

时间:2021-03-29 06:58:23

标签: excel vba pdf directory

Sub CheckandSend()
    Dim strfile As String
    Dim ws As Worksheet 'make sure to define a sheet
    Set ws = ThisWorkbook.Worksheets("RFQ")
    Sheets("RFQ").Select
    Dim Sourcepath As String
    Sourcepath = "P:\CENTRAL PLANNING\PROJECTS 2020-2021\VALEO FRANCE  35101380FM\Drg folder\"  

    Dim destpath As String
    destpath = "P:\CENTRAL PLANNING\PROJECTS 2020-2021\VALEO FRANCE  35101380FM\New folder1\"
    'make sure paths end with \        
    Dim irow As Long
    Dim f As SearchFolders
    Dim filetype As String
    filetype = "*.pdf"
    irow = 7
    
    Do While ws.Cells(irow, 2) <> vbNullString
        Dim FileName As String
        FileName = Dir(Sourcepath & ws.Cells(irow, 2) & "*.pdf")
        'this will only go through pdf files
        
        Do While FileName <> vbNullString
        'if more files with the key word from ws.Cells(iRow, 2) exist copy all of them
            VBA.FileCopy Sourcepath & FileName, destpath & FileName
            'copy needs to be path AND filename
            FileName = Dir()
        Loop
        
        irow = irow + 1
    Loop
    Sheets("RFQ").Select
    Columns("AB:AG").Select
    Selection.Delete

End Sub

在这里,此代码帮助我找到一个与我的 excel 单元格数据匹配的 pdf 文件,然后找到一个文件,它将复制该特定文件并粘贴到目标文件夹中

我的问题是我在 excel 中的文件名看起来像 AVRO15VE1522A, AVRO15VE1523B, AVRO15VE1524C , (在文件名列表中,最后一个字母是修订版它可能会变成A,B,C,D,E.....Z)

但在我的文件夹中,文件名就像 AVRO15VE1522A,(或)AVRO15VE1522A, AVRO15VE1523 B,(或)AVRO15VE1523B, 有时会出现多个具有相同文件名的文件,所以我只想要该特定文件名上的一个文件

如果所有的修订文件都是 AVRO15VE1522A, AVRO15VE1522B, AVRO15VE1522C, 存在于文件夹中意味着,我希望代码采用与我在 excel 范围内的文件名相匹配的文件

0 个答案:

没有答案
相关问题