+ Reply to Thread
Results 1 to 14 of 14

Problems with looping macro through specific folder

Hybrid 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

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Problems with looping macro through specific folder

    Alittle trick I learned from snb,
    Make sure that you only have the files you want to loop through in the folder. Change the folder path to suit your situation. Also make sure the master file in not in the folder to be looped through.

    Sub d()
    
        Dim cel As Range
        Dim nFile, sh As Worksheet
       
          Application.ScreenUpdating = 0
            With CreateObject("scripting.filesystemobject")
                     For Each nFile In .getfolder("C:\YourFilePath").Files
                        With Workbooks.Open(nFile)
                          For Each sh In .Sheets
                             With sh
                                .Columns("A:A").ClearContents
                             End With
                          .Close True
                        End With
                      End If
                    Next
           End With
         Application.ScreenUpdating = 1
    End Sub
    Edit, I just saw that you named the macro clearcontents, you should not do that. I would change the name
    Last edited by JapanDave; 09-07-2012 at 11:20 PM.
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

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

    Re: Problems with looping macro through specific folder

    Hi i tried your macro but there seems to be an error

    Sub d()
    
        Dim cel As Range
        Dim nFile, sh As Worksheet
       
          Application.ScreenUpdating = 0
            With CreateObject("scripting.filesystemobject")
                     For Each nFile In .getfolder("C:\YourFilePath").Files
                        With Workbooks.Open(nFile)
                          For Each sh In .Sheets
                             With sh
                                .Columns("A:A").clearcontents
                             End With
                          .Close True
                        End With compile error here
                      End If
                    Next
           End With
         Application.ScreenUpdating = 1
    End Sub
    However essentially i want to learn how to use a macro in a master workbook to loop pre-exisiting macros by call the sub procs, is there anyway to work around it this way?

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Problems with looping macro through specific folder

    Hi, I amended JapanDave's code and there are no compile errors.
    Option Explicit
    
    Sub d()
    
        Dim cel As Range
        Dim nFile, sh As Worksheet
    
        Application.ScreenUpdating = 0
        With CreateObject("scripting.filesystemobject")
            For Each nFile In .getfolder("C:\YourFilePath").Files
                With Workbooks.Open(nFile)
                    For Each sh In .Sheets
                        With sh
                            .Columns("A:A").ClearContents
                        End With
                    Next sh
                    .Close True
                End With
            Next nFile
        End With
        Application.ScreenUpdating = 1
    End Sub

    kchm, don't forget to change your path here:
    For Each nFile In .getfolder("C:\YourFilePath").Files
    Last edited by Mordred; 09-08-2012 at 12:10 AM.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

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

    Re: Problems with looping macro through specific folder

    Hi thanks for your help i think the macro works but i the macro does not save the results of the clearcontents how do i add that?

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Problems with looping macro through specific folder

    Try
    Option Explicit
    
    Sub d()
    
        Dim cel As Range
        Dim nFile, sh As Worksheet
    
        Application.ScreenUpdating = 0
        With CreateObject("scripting.filesystemobject")
            For Each nFile In .getfolder("C:\YourFilePath").Files
                With Workbooks.Open(nFile)
                    For Each sh In .Sheets
                        With sh
                            .Columns("A:A").ClearContents
                        End With
                    Next sh
                    .Save
                    .Close True
                End With
            Next nFile
        End With
        Application.ScreenUpdating = 1
    End Sub

  7. #7
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Problems with looping macro through specific folder

    Thanks Mordred,
    Sorry, I forgot to take the end with out from your code.
    Get rid of the macro that is called "clearcontents", this is causing problems.
    Try this,
    Option Explicit
    
    Sub d()
    
        Dim cel As Range
        Dim nFile, sh As Worksheet
    
        Application.ScreenUpdating = 0
        With CreateObject("scripting.filesystemobject")
            For Each nFile In .getfolder("C:\Users\Owner\Desktop\新しいフォルダー").Files
                With Workbooks.Open(nFile)
                    For Each sh In .Sheets
                        With sh
                            .Columns(1).clearcontents
                        End With
                    Next sh
                    .Close True
                End With
            Next nFile
        End With
        Application.ScreenUpdating = 1
    End Sub

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

    Re: Problems with looping macro through specific folder

    Thanks the code now works, is there a way to code so that i can just call sub procedures because i still have other macros i want to loop through folders?

  9. #9
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Problems with looping macro through specific folder

    Just type in the procedure names where you want them run.

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

    Re: Problems with looping macro through specific folder

    Option Explicit
    
    Sub d()
    
        Dim cel As Range
        Dim nFile, sh As Worksheet
    
        Application.ScreenUpdating = 0
        With CreateObject("scripting.filesystemobject")
            For Each nFile In .getfolder("C:\Users\Owner\Desktop\新しいフォルダー").Files
                With Workbooks.Open(nFile)
                    Call clearcontents
                    .Close True
                End With
            Next nFile
        End With
        Application.ScreenUpdating = 1
    End Sub
    something like this? where clear contents is


    sub c()
    
    Dim sh as worksheets
    
     For Each sh In .Sheets
                        With sh
                            .Columns("A:A").ClearContents
                        End With
                    Next sh
    
    End Sub

  11. #11
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Problems with looping macro through specific folder

    Just use,
    But, why do you need to call a macro?
    Option Explicit
    
    Sub d()
    
        Dim cel As Range
        Dim nFile, sh As Worksheet
    
        Application.ScreenUpdating = 0
        With CreateObject("scripting.filesystemobject")
            For Each nFile In .getfolder("C:\Users\Owner\Desktop\新しいフォルダー").Files
                With Workbooks.Open(nFile)
                    Call c
                    .Close True
                End With
            Next nFile
        End With
        Application.ScreenUpdating = 1
    End Sub

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

    Re: Problems with looping macro through specific folder

    because i still have other macros to run and i am not very proficient at macros hence i want a general macro that links several subprocedures together, instead of prompting the forum boards continually. Thanks modred and dave

  13. #13
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Problems with looping macro through specific folder

    No problem, if the thread has given you the answer please don't forget to say thanks by clicking the star in the left hand corner and mark the thread solved.

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

    Re: Problems with looping macro through specific folder

    Hi, i have tried again using call sub method however the is an error on the sub c()

    sub c()
    
    Dim sh as worksheets
    
     For Each sh In .Sheets (error down here)
                        With sh
                            .Columns("A:A").ClearContents
                        End With
                    Next sh
    
    End Sub
    can someone clarify why there is an error when calling this sub proc?

    actually i have other sub sub procs such as trim all cells and also adding a date column based on a cell reference that is why i am more comfortable using a looping macro through folder and then calling sub procs thanks a lot

+ Reply to Thread

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