Simple VBA to open all files in a folder

mk000

Board Regular
Joined
Dec 10, 2004
Messages
234
I want to loop through a directory, say C:\TEMP, open each .xls file, put 'hello' in cell A1, close and save, move on to the next one.

I had this code before, but I need some expertise.

Thanks!!!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
hi!
You did not mention on which sheet will the word hello be placed in the workbook.
i pressumed that you want it in all sheets' A1.

see below.

Code:
Sub Hello()
Dim Bk As Workbook
Dim Sht As Worksheet
With Application.FileSearch
    .NewSearch
    .LookIn = "C:\temp"
    .SearchSubFolders = False
    .Filename = "*.xls"
    .MatchTextExactly = True
    .FileType = msoFileTypeAllFiles
End With

With Application.FileSearch
    If .Execute() > 0 Then
        For i = 1 To .FoundFiles.Count
            Set Bk = Application.Workbooks.Open(.FoundFiles(i))
            For Each Sht In Bk.Worksheets
                Sht.Range("A1").Value = "hello"
            Next Sht
            Bk.Save
            Bk.Close
        Next i
    Else
        MsgBox "There were no files found.", vbCritical, "Error 101"
    End If
End With

End Sub
 
Upvote 0
Thanks...I think this will get me started!! Though, I reserve the right to come back with questions ;). Thanks again!
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,185
Members
449,071
Latest member
cdnMech

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