Solved

I have an excel 2010 macro that does what I want. I would like to build upon it as I now use it for many things.

Posted on 2014-07-30
7
426 Views
Last Modified: 2014-08-03
I would like it to search for multiple strings in multiple work books and prompt me for what those strings are and point me to search within a folder. Currently I can only do one search at a time and I have to go in and edit the macro. I also have to edit the macro to look for the folder. A button to run the command, enter my folder and search strings will be nice. I sometimes have to search 10 items and it is time consuming. Any help will be appreciated.

Sub SearchFolders()
    Dim fso As Object
    Dim fld 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

    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    'Change as desired
    strPath = "\\10.41.39.24\cshare\Inventory as of 12-31-13"
    strSearch = "55234"

    Set wOut = Worksheets.Add
    lRow = 1
    With wOut
        .Cells(lRow, 1) = "Workbook"
        .Cells(lRow, 2) = "Worksheet"
        .Cells(lRow, 3) = "Cell"
        .Cells(lRow, 4) = "Text in Cell"
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set fld = fso.GetFolder(strPath)

        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
                        lRow = lRow + 1
                        .Cells(lRow, 1) = wbk.Name
                        .Cells(lRow, 2) = wks.Name
                        .Cells(lRow, 3) = rFound.Address
                        .Cells(lRow, 4) = rFound.Value
                    End If
                    Set rFound = wks.Cells.FindNext(After:=rFound)
                Loop While strFirstAddress <> rFound.Address
            Next

            wbk.Close (False)
            strFile = Dir
        Loop
        .Columns("A:D").EntireColumn.AutoFit
    End With
    MsgBox "Done"

ExitHandler:
    Set wOut = Nothing
    Set wks = Nothing
    Set wbk = Nothing
    Set fld = Nothing
    Set fso = Nothing
    Application.ScreenUpdating = True
    Exit Sub

ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
End Sub
0
Comment
Question by:thanatos05
  • 3
  • 2
7 Comments
 
LVL 10

Expert Comment

by:Ray
ID: 40229612
Hope this helps a bit.  Don't think it addresses ALL of your wishes.

   'Change as desired
    strPath = sheet1.Range("Q2").Value        
         --> replace "\\10.41.39.24\cshare\Inventory as of 12-31-13" with
               CELL location where you can just type the value
               where sheetX is the sheet number where you'll type
               the value (sheet numbers can be found in VBA on the left usually)
    strSearch = do the same thing here as the strPath
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40229803
Hi,

The attached workbook contains a module with your slightly-modified code and an input form that will let you browse and identify the folder in which to search.  The folder MUST have at least one Excel file (*.xls*) in it in order to work.  When running, you select any Excel file in the folder; the subroutine will process all files as it currently does.

There is also a VBA InputBox method that prompts you for the search string.

I tested this on a small folder with about a dozen files; seemed to work as expected.  Let me know if you have any issues.

Regards,
-Glenn
EE-Q-28486782.xlsm
0
 

Author Comment

by:thanatos05
ID: 40230687
Thank you Glenn Ray, it does exactly what I need. Is there a way to search multiple strings instead of one?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40230708
I'm sure that could be done, but it would require significant changes to the code outside the scope of this question.  For one, the InputBox method would have to be replaced with something entirely different.

If you'll open a new question focused on that aspect (i.e., multiple string searches) I'm sure that I or any other Expert here can help resolve it.

Regards,
-Glenn
0
 

Author Comment

by:thanatos05
ID: 40230713
thank you all for your help.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40230806
You're welcome.  I'll keep a look out for a follow-up question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now