Hi, i have a macro to clear contents on all sheets on Col A.
Sub clearcontents()
Dim S_heet As Worksheet
For Each S_heet In ActiveWorkbook.Sheets
With S_heet
.Columns("A:A").clearcontents
End With
Next S_heet
End Sub
this macro works when i place it in a specifc workbook to run it. However when i try calling it using this macro to loop through all workbooks in specific folder it doesn't work. The looping macro is as below. Can someone show me where i went wrong? Master represents the workbook which the looping macro is running from and Book1 and Book2 are the files that i want to clear contents with.
Sub RunCodeOnAllXLSFiles()
Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook
'Application.ScreenUpdating = False
'Application.DisplayAlerts = False
'Application.EnableEvents = False
On Error Resume Next
Set wbCodeBook = ThisWorkbook
With Application.FileSearch
.NewSearch
'Change path to suit
.LookIn = "C:\Documents and Settings\Administrator\Desktop\Test"
.FileType = msoFileTypeExcelWorkbooks
'.Filename = "Book*.xls"
If .Execute > 0 Then 'Workbooks in folder
For lCount = 1 To .FoundFiles.Count 'Loop through all.
'Open Workbook x and Set a Workbook variable to it
Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
Call clearcontents
wbResults.Close SaveChanges:=True
Next lCount
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
Bookmarks