Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

excel - Loop Through Sub-Dir

Sub CheckandSend()
    Dim strfile As String
    Dim ws As Worksheet 'make sure to define a sheet
    Set ws = ThisWorkbook.Worksheets("RFQ")
    Sheets("Part list").Select
    Sheets("RFQ").Select
    Dim lastrow As Long
    lastrow = ActiveSheet.Cells(Rows.Count, 2).End(xlUp).Row
    Range("A6:E" & lastrow).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Part list").Select
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Cells.Select
    Cells.EntireColumn.AutoFit
Dim Worksheet2_Name As String
WorkRFQ_Name = "Part list" ' Replace this with the name of the first sheet you want to export
Set WorkRFQ = ThisWorkbook.Worksheets(WorkRFQ_Name)
Dim Write_Directory As String
Dim WorkRFQ_Path As String
Write_Directory = "P:CENTRAL PLANNINGPROJECTS 2020-2021VAM-TARSONNewfolder1" 
WorkRFQ_Path = Write_Directory & "" & WorkRFQ_Name
WorkRFQ.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    FileName:=WorkRFQ_Path, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
    
    Sheets("Part list").Select
    Columns("A:Z").Select
    Selection.Delete
    Sheets("RFQ").Select
    Dim SourcePath As String
    SourcePath = "I:MechanicalExternalProjectsCummins Emission Systems35101124 PT Cup Test Rig16 PDF to Vendor"
    Dim DestPath As String
    DestPath = "P:CENTRAL PLANNINGPROJECTS 2020-2021VAM-TARSONNewfolder1"

    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")
       
        Do While FileName <> vbNullString
            VBA.FileCopy SourcePath & FileName, DestPath & FileName
            FileName = Dir()
        Loop
        
        irow = irow + 1
    Loop
end sub

On here, this code is help me to find an pdf file which is present in my sourcepath & plug that file & place in my destpath now where i am lagging is, in my sourcepath (pdf to vendor") after this folder there are multiple sub-folders , i want a code which loop through all sub-folders and find my files and place it in my dest path

my sub folders will be look like OP10, OP20, OP30.....ETC...,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a function which will return a collection of matching file objects given a starting location and a file name pattern:

'Return a collection of file objects given a starting folder and a file pattern
'  e.g. "*.txt"
'Pass False for last parameter if don't want to check subfolders
Function FileMatches(startFolder As String, filePattern As String, _
                    Optional subFolders As Boolean = True) As Collection

    Dim fso, fldr, f, subFldr
    Dim colFiles As New Collection  '<< all matched files
    Dim colSub As New Collection    '<< folders to be scanned for matching files
    
    Set fso = CreateObject("scripting.filesystemobject")
    colSub.Add startFolder   '<< add the starting folder
    
    'loop while there are still folders to be scanned
    Do While colSub.Count > 0
        Set fldr = fso.getfolder(colSub(1))
        colSub.Remove 1   '<< remove the folder we're now scanning from the list
        For Each f In fldr.Files 'get files in folder
            'if the filename is like the pattern, add to the "hits" collection
            If UCase(f.Name) Like UCase(filePattern) Then colFiles.Add f
        Next f
        If subFolders Then 'get subfolders for processing?
            For Each subFldr In fldr.subFolders
                colSub.Add subFldr.Path  '<< add subfolder to list for processing
            Next subFldr
        End If
    Loop
    Set FileMatches = colFiles 'return all matched files
End Function

Example usage based on your posted code:


    Dim SourcePath As String, DestPath As String
    Dim colFiles as Collection, f

    SourcePath = "I:MechanicalExternalProjectsCummins Emission Systems35101124 PT Cup Test Rig16 PDF to Vendor"
    
    DestPath = "P:CENTRAL PLANNINGPROJECTS 2020-2021VAM-TARSONNewfolder1"

    Dim irow As Long
    Dim f As SearchFolders
    Dim filetype As String
    filetype = "*.pdf"
    irow = 7
    
    Do While ws.Cells(irow, 2) <> vbNullString
        
        Set colFiles = FileMatches(SourcePath, ws.Cells(irow, 2) & "*.pdf")
        For Each f in colFiles
            f.Copy DestPath & f.Name
        Next f
        
        irow = irow + 1
    Loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...