• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Search Macro

Tucker1050

New Member
I am trying to search a directory at work (folders / sub-folders) for items listed in an excel workbook to see if those items are contained in any other excel file in the directory. Does anyone have a Macro that could do this? It would be great if the macro could return the path and file name of each searched item.

Thanks in advance for any help.
 
Hello SirJB87. Thanks for the quick response. The items that I am referring to are words or phrases. To be more specific, I am working with SAP and have a list of document numbers ("100001321" for example). I want to see if "100001321" exists anywhere in the folder/sub-folder of multiple excel files. What would be great if the macro could tell the path and what excel file the searched item is in. BTW, I am working on Windows 7 and Excel 2010.
 
Hi, Tucker1050!
When you say that your items refer to word or phrases, do you mean as part of a filename or as text within an Excel file (i.e., cell contents).
If it's the 1st, the file at the 2nd link has a module called modFoldersFilesRecursive: there you'll find all but filtering by an argument, it's just adding an If with a Like "*" & Argument & "*" to check against the filename.
If it's the 2nd, I don't know any macro to do that and I wouldn't do it neither by macro nor with Excel; I'd choose any software that searches for file contents.
Regards!
 
Thanks for the info SirJB7. I am looking for the 2nd one that you are referencing. Do you have any software recommendations? If all else fails...I know what files to search, so I may just write a long look up formula.
 
Hello.

I can give you a direction to this.

You will have to build two loops.

1. Loop through the files that you want to search from. The link posted in the above thread will help you do that.

2. Open each workbook and find the "text" you need, if it exists return the address and path.

Also to counter multiple appearance in a workbook, you will have to loop till the find method comes to the first found address within the workbook.

I know its a bit cumbersome, but it is doable
 
Thanks BBD!! I am really new to VBA. Could you provide me with the code and I could play around with it? Basically, I would be listing a series of text items in column A and if the search results are positive to display the path and filename in columns B and C respectively. It would be even better if the cell reference could be displayed too in column D (optional). If the results do not return anything for a particular item, then a message like "Item not found" or something of the sort should be displayed in any column B-D. Would this be possible? Really my first time with VBA doing this and trying to get my feet wet.

Thanks again!
 
I know its a bit cumbersome, but it is doable
Hi, Tucker1050!
I think you're in good hands :), or should it be that I don't want to get my hands so dirty? :rolleyes:
The procedure described by BBD it's the correct one, didn't check the uploaded file, but give it a try.
Regards!

@BBD
Hi!
Thanks for taking the baton... enjoy it.
Regards!
 
Hi, Tucker1050!

This is a sample code for the Find trick of not eternally looping.

Code:
Option Explicit

Sub ExtractListFromTable()
    ' constants
    Const ksSourceWS = "Sheet1"
    Const ksSourceRange = "SourceTable"
    Const ksTargetWS = "Sheet2"
    Const ksTargetRange = "TargetList"
    Const ksArgument = "ArgumentCell"
    ' declarations
    Dim rngS As Range, rngT As Range, c As Range
    Dim vArgument As Variant
    Dim I As Long, A As String
    ' start
    Set rngS = Worksheets(ksSourceWS).Range(ksSourceRange)
    Set rngT = Worksheets(ksTargetWS).Range(ksTargetRange)
    vArgument = Worksheets(ksTargetWS).Range(ksArgument)
    With rngT
        If .Rows.Count > 1 Then Range(.Rows(2), .Rows(.Rows.Count)).ClearContents
    End With
    I = 1
    ' process
    With rngS
        Set c = .Find(vArgument, .Cells(1, 1), xlValues, xlPart, xlByColumns, , True)
        Do Until c Is Nothing
            ' save 1st found
            If A = "" Then A = c.Address
            ' add entry
            I = I + 1
            rngT.Cells(I, 1).Value = c.Value
            ' cycle
            Set c = .FindNext(c)
            ' check if restarted
            If A = c.Address Then Exit Do
        Loop
    End With
    ' end
    Set rngT = Nothing
    Set rngS = Nothing
    Beep
End Sub

Source:
http://chandoo.org/forum/threads/please-help-to-extract-the-datas-from-group-data.13472

Regards!
 
Hi, Tucker1050!
Glad to help. All credit to BBD. Thanks for your feedback welcome back whenever needed or wanted.
Regards!
 
Hi BD,

I am in search of another macro to unhide and close & don't save all hidden workbooks except my "Personal (3).xlsb" workbook since it has all my macros. The code I found is below:

Public Sub UnhideWorkbooks()
Dim i As Integer
For i = 1 To Workbooks.Count
If Application.Windows(i).Visible = False Then
Application.Windows(i).Visible = True
End If
Next
End Sub

How do I incorporate skipping over the Personal Workbook and adding the close and don't save function?

As always, thank you in advance for your help.
 
@Tucker1050
Hi!

Would you please start a new topic instead of writing on a topic that despite of being started by you it's related to other subject? If needed you could add a reference in your new one.

Regards!

PS: Please don't answer here at this thread.
 
Hello,
I was searching for macro to do something similar and the solution by BBD does it perfectly. However, I would like the macro to stop at the first instance of finding the "text" in a file. Can you please help me by letting me know how I can modify the macro to stop after it finds the "text" the first time in a file and move on to the next file?
Not sure if the macro looks into sub folders?
Any help is appreciated.
 
Back
Top