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
432 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

831 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