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
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
434 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

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;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

860 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