Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 08-19-2023, 09:00 AM
ranjan ranjan is offline Copying files from multiple subfolders using vba Windows 10 Copying files from multiple subfolders using vba Office 2019
Advanced Beginner
Copying files from multiple subfolders using vba
 
Join Date: May 2021
Posts: 80
ranjan is on a distinguished road
Default

Hi,

I had tried the below one's not getting work...

Please check once and do the needful.

I need to copy files from sub-folder & folder to another location, i want it to "recurse" through as many levels as it finds?

Column A is file names, B is status updation if it is available update as "available" else "n/a" (A1,B1 are headings, From A2 to A100 & B2 to B100 are the range)

Files names will be excluding the extenstions in column "A". if a file name entered in A2 as 123 and update status in B2 as "available".
I want to copy all the files with the same file name with different extentions as 123.pdf, 123.rtf, 123.docx to a targeted folder, similarly File name for A3 456 ,A4 789 and copy all the formats if the file name exists in folders and sub folders....and same update the status in B2)

Code:
Sub CopyFilesBasedOnXcelValue()
    Dim srcFolder As String
    Dim destFolder As String
    Dim fileTypes As Variant
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    
    ' Input folder location
    srcFolder = InputBox("Enter the source folder location:")
    
    ' Destination folder location
    destFolder = InputBox("Enter the destination folder location:")
    
    ' File types to search for
    fileTypes = Array("*.pdf", "*.doc", "*.docx", "*.rtf", "*.xls", "*.xlsx")
    
    ' Set the worksheet to the active sheet
    Set ws = ActiveSheet
    
    With ws
        ' Get the last row in column A
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        
        ' Loop through each row in column A
        For i = 1 To lastRow
            ' Check if the file is available
            If FileExists(srcFolder & "\" & .Cells(i, "A").Value, fileTypes) Then
                ' Copy the file to the destination folder
                FileCopy srcFolder & "\" & .Cells(i, "A").Value, destFolder & "\" & .Cells(i, "A").Value
                ' Update the status in column B
                .Cells(i, "B").Value = "Available"
            Else
                ' Update the status in column B
                .Cells(i, "B").Value = "N/A"
            End If
        Next i
    End With
End Sub

Function FileExists(filePath As String, fileTypes As Variant) As Boolean
    Dim fileName As String
    Dim fileType As Variant
    Dim filesFound As Boolean
    Dim i As Long
    
    ' Check if the file exists with any of the specified file types
    For Each fileType In fileTypes
        fileName = Dir(filePath & fileType)
        If Len(fileName) > 0 Then
            filesFound = True
            Exit For
        End If
    Next fileType
    
    FileExists = filesFound
End Function

Last edited by ranjan; 08-19-2023 at 12:53 PM. Reason: Adding Info
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch change the font style in multiple word files inside folder and subfolders kalagas Word VBA 11 10-05-2023 05:13 AM
Pls help - copying data from multiple word files handclips Word VBA 3 01-24-2021 01:57 PM
Copying files from multiple subfolders using vba List all word files in folder and subfolders eduzs Word VBA 5 06-09-2019 06:20 AM
Searching through folders/ subfolders and rename files if certain condition is met mihnea96 Excel 1 05-15-2017 07:09 AM
Copying files from multiple subfolders using vba Copying multiple files as text without extensions Metamag Office 3 05-09-2011 06:25 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:58 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft