?
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
Medium Priority
?
445 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 11

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 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

770 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