Loop thru Subdirectories find all .xls update value of specific cells

nhnana

New Member
Joined
Dec 28, 2010
Messages
3
Hello, I need code to loop thru all subdirectories open each workbook in the subdirectory that contains as part of the file name "_Sales Order.xls" and open each "_Sales Order.xls" and update worksheet "Sales Order Form" the following values:
C44 new value = "00-440"
D44 new value = " a few words"
H44 new value = "00-440"

Any help is very much appreciated! Directory structure looks like
N:\\Customers\ABC\January_Sales Order.xls
Thank you so much.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try...

Code:
Option Explicit

Sub test()

    Dim objFSO As Object
    Dim objFolder As Object
    Dim objSubFolder As Object
    Dim objFile As Object
    Dim MyPath As String
    Dim wkbOpen As Workbook
    Dim Cnt As Long
    
    Application.ScreenUpdating = False
    
    MyPath = "N:\\Customers"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    Set objFolder = objFSO.GetFolder(MyPath)
    
    For Each objSubFolder In objFolder.SubFolders
        For Each objFile In objSubFolder.Files
            If LCase(objFile.Name) Like "*_sales order.xls" Then
                Set wkbOpen = Workbooks.Open(objFile)
                With wkbOpen.Worksheets("Sales Order Form")
                    .Range("C44").Value = "00-440"
                    .Range("D44").Value = "a few words"
                    .Range("H44").Value = "00-440"
                End With
                wkbOpen.Close savechanges:=True
                Cnt = Cnt + 1
            End If
        Next objFile
    Next objSubFolder
    
    Application.ScreenUpdating = True
    
    If Cnt = 0 Then
        MsgBox "No '_sales order.xls' files found...", vbInformation
    Else
        MsgBox "Completed...", vbInformation
    End If
    
End Sub
 
Upvote 0
Domenic, Thank you so very much, I will try it right now - I also appreciate the quick response !
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top