+ Reply to Thread
Results 1 to 6 of 6

Running search in multiple files in a folder

Hybrid View

  1. #1
    Registered User
    Join Date
    12-09-2014
    Location
    Prague, Czech Republic
    MS-Off Ver
    2010
    Posts
    4

    Running search in multiple files in a folder

    Hello everybody,

    I`d like to ask you for some help.
    I`m cleaning log files. The aim is to find all zeros in column B and detele any value in adjacent cell in column A. And I`d like to do that in multiple files in a folder (called ZkouskaMakra).

    My current code fails to work. When it reaches Set F1 it performs in the file I`m running it from not in the file I want to run it in.
    ( I tried substituting .Font.Bold = True for the part between Set F1 and End If ... that worked in all the files. That makes me think that till With wb... it is correct)

    Please tell me what`s wrong


    Sub MultipleFileMacro()

    Dim wb As Excel.Workbook
    Dim fso As Scripting.FileSystemObject
    Dim f As Scripting.File

    Dim F1 As Range
    Dim F2 As String

    Set fso = New Scripting.FileSystemObject
    For Each f In fso.GetFolder("D:\ZkouskaMakra").Files
    Set wb = Workbooks.Open(f)

    With wb.Worksheets("List1").Range("b2", "b5000")
    Set F1 = .Find(What:=0, LookIn:=xlValues, LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext)


    If Not F1 Is Nothing Then
    F2 = F1.Address
    Do
    Cells(F1.Row, F1.Column - 1) = ""
    Set F1 = .FindNext(F1)
    Loop Until F1.Address = F2
    End If
    End With


    wb.Save
    wb.Close
    Next f

    End Sub

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Running search in multiple files in a folder

    Perhaps a macro similar to this:

    Option Explicit
    
    Sub ModLog()
    
     Dim sPath As String
     Dim Wb As Workbook
     Dim sFile As String
     Dim i As Integer
     Dim j As Integer
     
        sPath = "D:\ZkouskaMakra\"
        sFile = Dir(sPath & "*.xl??")
        Application.ScreenUpdating = False
            Do While sFile <> ""
            Set Wb = Workbooks.Open
            On Error Resume Next
            Sheets("List1").Activate
            If Err.Number = 9 Then
                Wb.Close SaveChanges:=False
                GoTo skipper
            End If
          If Range("B1") = "" Then
                Range("B1") = "Val"
          End If
        Range("B1:B5000").AutoFilter Field:=1, Criteria1:=0
        
        j = Range("A1").End(xlDown).Row
        i = Range("A" & Rows.Count).End(xlUp).Row
        
      Range("A" & j & ":A" & i).ClearContents
      ActiveSheet.AutoFilterMode = False
            Wb.Close SaveChanges:=True
    skipper:
        sFile = Dir
        Loop
       Application.ScreenUpdating = True
    End Sub
    Using autofilter on range B1:B5000 and this will capture all values of zero in the range B2:B5000 but there should be a text/heading in cell B1, if blank macro will set it. After filering getting all 0 values i column B the values in the A column is deleted and the file saved.

    I would suggest you first test this macro on a small sample of files and checks that it works the way you wish.

    Alf

  3. #3
    Registered User
    Join Date
    12-09-2014
    Location
    Prague, Czech Republic
    MS-Off Ver
    2010
    Posts
    4

    Re: Running search in multiple files in a folder

    Thanks a lot. I tried your code but it didn`t work.
    It announced at Set Wb = Workbooks.Open Argument not optional.

    As I`m a coding muggle and I didn`t want to spend time fixing the part which in the original code worked I went back to that code.
    In the end the aim changed a bit (not deleting the value in the adjacent cell but replacing the zero by E20). Then we found problem with the target sheets being called differently in each file. Anyway this, even though it might not be nice or strictly correct, works:

    Sub MonitoringCisteni()
    
    Dim Wb As Excel.Workbook
    Dim fso As Scripting.FileSystemObject
    Dim f As Scripting.File
    Dim AdresaSlozky As String
    
    Dim F1 As Range
    Dim F2 As String
    
    AdresaSlozky = InputBox("Zadejte plnou adresu složky ve formátu S:\3A14119_plavebni_stupen_decin_experimentalni_vyhony\Podklady\Prijate\3A12042_87_12042_vyhonydvz")
    
    Application.ScreenUpdating = False 
    Set fso = New Scripting.FileSystemObject
        For Each f In fso.GetFolder(AdresaSlozky).Files
        Set Wb = Workbooks.Open(f)
        
        With Wb
            With ActiveSheet.Range("B2", "B5000")
            Set F1 = .Find(What:=0, LookIn:=xlValues, LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext)
            
            If Not F1 Is Nothing Then
                Do While Not F1 Is Nothing
                    F1 = "E20"
                     Set F1 = .FindNext(F1)
                Loop
            End If
            End With
        End With
          
        Wb.Close SaveChanges:=True
    Next f
    Application.ScreenUpdating = True
    End Sub
    Just if anyone random was interested
    Oridoll

  4. #4
    Registered User
    Join Date
    12-09-2014
    Location
    Prague, Czech Republic
    MS-Off Ver
    2010
    Posts
    4

    Re: Running search in multiple files in a folder



    the part with AdresaSlozky = InputBox("Zadejte plnou adresu složky ve formátu S:\3A14119_plavebni_stupen_decin_experimentalni_vyhony\Podklady\Prijate\3A12042_87_12042_vyhonydvz")
    is just inbox for the target file adress ... I can`t edit it in my previous post for some reason

  5. #5
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Running search in multiple files in a folder

    My bad! The string should of cource be

    Set Wb = Workbooks.Open(sPath & sFile)
    But as you have fixed you problem this is rather superfluous information.

    Alf

  6. #6
    Registered User
    Join Date
    12-09-2014
    Location
    Prague, Czech Republic
    MS-Off Ver
    2010
    Posts
    4

    Re: Running search in multiple files in a folder

    Cool thanks a lot. It might come useful sometime in the future.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Excel VBA code for selecting a folder then search & open specified files in that folder
    By Excelrookie_1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-27-2021, 03:09 AM
  2. Replies: 1
    Last Post: 03-12-2013, 04:45 AM
  3. Running A macro through multiple files in a folder
    By Briansva92 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-31-2012, 01:26 PM
  4. Replies: 3
    Last Post: 02-22-2012, 11:40 AM
  5. Search files in folder based on search string
    By djvice in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-22-2008, 01:12 AM

Tags for this Thread

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