Results 1 to 14 of 14

Problems with looping macro through specific folder

Threaded View

  1. #1
    Registered User
    Join Date
    07-22-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    85

    Problems with looping macro through specific folder

    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
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1