+ Reply to Thread
Results 1 to 11 of 11

Case insensitive search for string within folder of excel files

Hybrid View

  1. #1
    Registered User
    Join Date
    02-18-2011
    Location
    cork
    MS-Off Ver
    Excel 2003
    Posts
    15

    Smile Case insensitive search for string within folder of excel files

    I need to be able to do a case insensitive search for a string in a specific folder containing multiple excel files none of which have the same number of worksheets.

    The result of the search should be made available in a new file which will specify where the matches were found.

    I look forward to your kind help in this regard.
    Last edited by vba_novice; 03-22-2011 at 11:26 PM. Reason: confirm SOLVED (Thank you so much JBeaucaire)

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Case insensitive search for string within folder of excel files

    Searching filenames in a specific folder is inherently a case-non-sensitive event. MYFILE.xls and MyFile.xls would register as the same file.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-18-2011
    Location
    cork
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Case insensitive search for string within folder of excel files

    Dear JBeaucaire, What I actually need to do is examine a column of values in SHEET1, of a specific excel file and then identify the occurrences of all of these values inside the worksheets of a bunch of other excel files (all having multiple sheets) located in a single folder. Your assistance will be highly appreciated. Thank you.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Case insensitive search for string within folder of excel files

    Without referring to macros or "method", go and layout step by step how you would do this manually, be very specific.

    1) go to folder C:\MyFiles\
    2) Open file#1 (we will eventually check every file in this folder)
    3) Search "Sheet1" column A for "cat" (partial string match or entire cell match?)
    4) If found, write down the filename and cell reference in report
    5) Continue searching that file for more instances of "cat", note each one
    6) Close file

    7) Repeat steps 2-7 for every file in folder.


    Something like that, except make it 100% accurate for your steps.
    Last edited by JBeaucaire; 03-22-2011 at 12:30 PM.

  5. #5
    Registered User
    Join Date
    02-18-2011
    Location
    cork
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Case insensitive search for string within folder of excel files

    Dear JBeaucaire, Thank you. Let me try this.

  6. #6
    Registered User
    Join Date
    02-18-2011
    Location
    cork
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Case insensitive search for string within folder of excel files

    Dear JBeaucaire,

    I have pasted here the code. I get the "Object required" error.

    1. I have 3nos files located in E:\0_test search. Each file contains multiple sheets.
    2. SearchMultipleFiles.xls contains Sheet1 which contains a string in each of the cells in range A1:A4
    3. I wish to sequentially search for each one of these strings (mentioned in item-2 above) in each of the 3nos file located in E:\0_test search.
    4. I wish to dump the found strings into SearchMultipleFiles.SearchResults

    Sub SearchFolders()
        Dim fso As Object
        Dim fld As Object
        Dim sfl As Object
        Dim strSearch As String
        Dim strPath As String
        Dim strFile As String
        Dim wOut As Worksheet
        Dim wbk As Workbook
        Dim wks As Worksheet
        Dim lRow As Long
        Dim rFound As Range
        Dim strFirstAddress As String
        Dim iNoOfRowsInSpreadsheet As Integer
        Dim iCellWithTag2Search As Integer
        Dim iCellXaminRow As Integer
        Dim iCellXaminCol As Integer
        Dim wbThis As Workbook
        Dim wsTarget As Worksheet
            
            
        On Error GoTo ErrHandler
        Application.ScreenUpdating = False
        strPath = "e:\0_test search"
            
        iNoOfRowsInSpreadsheet = 4
        iCellXaminRow = 1
        iCellXaminCol = 1
        
        Sheets.Add Type:=xlWorksheet
        ActiveSheet.Name = "SearchResults"
            lRow = 1
            Cells(1, 1).Value = "Text in Cell"
            Cells(1, 2).Value = "File Name"
            Cells(1, 3).Value = "Sheet Name"
            Cells(1, 4).Value = "Cell Location"
        
        Worksheets("Sheet1").Select
               
        For iCellXaminRow = 1 To iNoOfRowsInSpreadsheet
        strSearch = Cells(iCellXaminRow, iCellXaminCol).Value
        
        
        With wOut
            strFile = Dir(strPath & "\*.xls*")
            Do While strFile <> ""
                Set wbk = Workbooks.Open _
                  (FileName:=strPath & "\" & strFile, _
                  UpdateLinks:=0, _
                  ReadOnly:=True, _
                  AddToMRU:=False)
    
                For Each wks In wbk.Worksheets
                    Set rFound = wks.UsedRange.Find(strSearch)
                    If Not rFound Is Nothing Then
                        strFirstAddress = rFound.Address
                    End If
                    Do
                        If rFound Is Nothing Then
                            Exit Do
                        Else
                            Set wbThis = ThisWorkbook
                            Set wsTarget = wbThis.Worksheets("SearchResults")
                            wshTarget.Cells(2, 1) = strSearch
                        End If
                        Set rFound = wks.Cells.FindNext(After:=rFound)
                    Loop While strFirstAddress <> rFound.Address
                Next
                wbk.Close (False)
                strFile = Dir
            Loop
            End With
        
        Next iCellXaminRow
        
        MsgBox "Done!!  Now see NEW sheet 'SEARCH RESULTS'"
        
    ExitHandler:
        Set wOut = Nothing
        Set wks = Nothing
        Set wbk = Nothing
        Set sfl = Nothing
        Set fld = Nothing
        Set fso = Nothing
        Application.ScreenUpdating = True
        Exit Sub
    
    ErrHandler:
        MsgBox Err.Description, vbExclamation
        Resume ExitHandler
    End Sub

  7. #7
    Registered User
    Join Date
    02-18-2011
    Location
    cork
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Case insensitive search for string within folder of excel files

    Dear JBeaucaire, I apologise but I cannot see the button for file attaching.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Case insensitive search for string within folder of excel files

    Go advanced, paperclip icon.

  9. #9
    Registered User
    Join Date
    02-18-2011
    Location
    cork
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Case insensitive search for string within folder of excel files

    Dear JBeaucaire,

    I should have inserted into the following line of code "iCellXaminRow" instead of "2" however, problems other than this exist. Thank you for your kind attention:

    wshTarget.Cells(2, 1) = strSearch
    Attached Files Attached Files

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Case insensitive search for string within folder of excel files

    Here's my take on that:
    Option Explicit
    
    Sub SearchFolders()
    Dim fPath   As String
    Dim fName   As String
    Dim wb      As Workbook
    Dim ws      As Worksheet
    Dim wsRpt   As Worksheet
    Dim wsData  As Worksheet
    Dim strRNG  As Range
    Dim MyStr   As Range
    Dim strFND  As Range
    Dim strFRST As Range
    Dim NR      As Long
    
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    fPath = "e:\0_test search\"       'remember \ at end of this string
    
    If Evaluate("ISREF(SearchResults!A1)") Then Sheets("SearchResults").Delete
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "SearchResults"
    With ActiveSheet.Range("A1:D1")
        .Value = [{"Text in cell","File Name","Sheet Name","Cell Location"}]
        .Font.Bold = True
        .Borders(xlEdgeBottom).Weight = xlMedium
        .Offset(1).Cells(1).Select
        ActiveWindow.FreezePanes = True
        NR = 2
    End With
    
    Set wsRpt = ThisWorkbook.Sheets("SearchResults")
    Set wsData = ThisWorkbook.Sheets("Sheet1")
    Set strRNG = wsData.Range("A:A").SpecialCells(xlConstants)
        
        fName = Dir(fPath & "\*.xls*")
        
        Do While Len(fName) > 0
            If fName <> ThisWorkbook.Name Then
                Set wb = Workbooks.Open(FileName:=fPath & fName)
        
                For Each ws In wb.Worksheets
                    For Each MyStr In strRNG
                        Set strFND = ws.UsedRange.Find(MyStr)
                        If Not strFND Is Nothing Then
                            Set strFRST = strFND
                            Do
                                wsRpt.Range("A" & NR) = strFND
                                wsRpt.Range("B" & NR) = fName
                                wsRpt.Range("C" & NR) = ws.Name
                                wsRpt.Range("D" & NR) = strFND.Address
                                Set strFND = ws.Cells.FindNext(strFND)
                                NR = NR + 1
                            Loop While strFND.Address <> strFRST.Address
                        End If
                        Set strFND = Nothing
                    Next MyStr
                Next ws
                wb.Close (False)
            End If
            fName = Dir
        Loop
        
        MsgBox "Done!!  Now see NEW sheet 'SEARCH RESULTS'"
        wsRpt.Columns.AutoFit
        
    ExitHandler:
        Application.ScreenUpdating = True
        Exit Sub
    
    ErrHandler:
        MsgBox Err.Description, vbExclamation
        Resume ExitHandler
    End Sub

  11. #11
    Registered User
    Join Date
    02-18-2011
    Location
    cork
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Case insensitive search for string within folder of excel files

    Dear JBeaucaire,

    You great help is very highly appreciated!
    Your code is working very well.
    Thank you for your generousity in time and expertise.
    My very best wishes for you and yours.

+ 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