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
423 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

19 Experts available now in Live!

Get 1:1 Help Now