+ Reply to Thread
Results 1 to 10 of 10

Marco to Open all contents (.xlsm) in a folder and search each file for a word

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2014
    Location
    Ontario,Canada
    MS-Off Ver
    Windows 7
    Posts
    4

    Marco to Open all contents (.xlsm) in a folder and search each file for a word

    Hi all,

    I'm looking for help to create Macro to open contents in folder and search for the word "troubleshoot" in each file. The folder contains 287; having to open all the files is time consuming.

    Thanks in advance.

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Marco to Open all contents (.xlsm) in a folder and search each file for a word

    So this macro will open all files in a folder:

    Sub OpenFiles()
    Dim MyFolder As String
    Dim MyFile As String
    MyFolder = "\\G:\Functional Teams\Finance\2013\Consolidation & Reporting\1013\Financial Package"
    MyFile = Dir(MyFolder & "\*.xls")
    Do While MyFile <> ""
    Workbooks.Open Filename:=MyFolder & "\" & MyFile
    MyFile = Dir
    Loop
    End Sub
    but I don't know what you want the code to do WHEN/IF it finds troubleshoot anywhere. What are you wanting at that point?

    Also is there a set number of sheets in each file, do they have only one sheet or multiple variable worksheets?
    Last edited by mikeTRON; 07-11-2014 at 12:38 PM.
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Forum Contributor Jack7774's Avatar
    Join Date
    08-31-2012
    Location
    Georgia, US
    MS-Off Ver
    Excel 2010
    Posts
    576

    Re: Marco to Open all contents (.xlsm) in a folder and search each file for a word

    This will open all folders and search each cell in columns A:IV for the word troubleshoot however exactly what miektron said what do you want it to do when you find the word?

    Sub openallfilesinfolder()
    
    Dim MyFolder As String
    Dim MyFile As String
    MyFolder = "C:\Users\JBRANUM\Desktop\New Folder"
    MyFile = Dir(MyFolder & "\*.csv")
    
    Do While MyFile <> ""
        Workbooks.Open Filename:=MyFolder & "\" & MyFile
        MyFile = Dir
        
        Dim ws As Worksheet
        For Each ws In ActiveWorkbook.Worksheets
        
        
        Dim FindString As String
            Dim Rng As Range
            FindString = "troubleshoot"
            If Trim(FindString) <> "" Then
                With ActiveSheet.Range("A:IV")
                    Set Rng = .find(What:=FindString, _
                                    After:=.Cells(.Cells.Count), _
                                    lookin:=xlValues, _
                                    LookAt:=xlWhole, _
                                    SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext, _
                                    MatchCase:=False)
                    If Not Rng Is Nothing Then
                        Application.Goto Rng, True
                    Else
                        MsgBox "Nothing found"
                    End If
                End With
            End If
        Next ws
    Loop
    
    
    
    End Sub
    Thank those who have helped you by clicking the Star * below their name and please mark your post [SOLVED] if it has been answered satisfactorily.

  4. #4
    Registered User
    Join Date
    06-26-2014
    Location
    Ontario,Canada
    MS-Off Ver
    Windows 7
    Posts
    4

    Re: Marco to Open all contents (.xlsm) in a folder and search each file for a word

    Thanks guys!

    After finding the files that contain the word "troubleshoot", I'd like to add the contents in Cell E23 into a new workbook/the workbook that contain the Macro(above).

    Thanks again.

  5. #5
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Marco to Open all contents (.xlsm) in a folder and search each file for a word

    Does troubleshoot always fall in ONE particular cell?
    Are there multiple sheets in each workbook?
    What else can you tell us to help fine tune this thing?

    Can you post a sample dataset to illustrate what you are wanting?

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Marco to Open all contents (.xlsm) in a folder and search each file for a word

    This checks all sheets in all files in a folder and puts the file names and cell addresses in a range.
    The workbook with this code in it should not be in the same folder.

    Sub CheckFiles()
        Const fPath As String = "E:\Some Folder\Some Folder\"    '<----- Change as required
        Dim sh As Worksheet
        Dim sName As String
        Dim fnd As Range
        Dim sw As String
        Dim a As String
        With Application
            .Calculation = xlCalculationManual
            .EnableEvents = False
            .ScreenUpdating = False
        End With
    
        sName = Dir(fPath & "*.xls*")
        sw = ActiveSheet.Range("B6").Value    '<----- B6 has the word you're looking for
        Do Until sName = ""
            With GetObject(fPath & sName)
                For Each sh In .Worksheets
                    With sh
                        Set fnd = sh.Columns(1).Find(what:=sw, Lookat:=xlPart, MatchCase:=False)
                        If Not fnd Is Nothing Then Cells(Rows.Count, 2).End(xlUp).Offset(1) = "Found in " & sName & _
                        " in cell " & fnd.Address & " in sheet " & sh.Name
                        'If Not fnd Is Nothing Then a = "Found in " & sName & " in Sheet " & sh.Name
                    End With
                Next sh
                .Close True
            End With
            sName = Dir
        Loop
        'Range("B10").Value = a
        With Application
            .Calculation = xlAutomatic
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub
    You can add the line to copy E23 into your workbook
    Sorry, it checks the first column only.
    You can change
    Set fnd = sh.Columns(1).Find(what:=sw, Lookat:=xlPart, MatchCase:=False)
    to this
    Set fnd = sh.Cells.Find(what:=sw, Lookat:=xlPart, MatchCase:=False)
    Last edited by jolivanes; 07-11-2014 at 01:35 PM. Reason: Add info

  7. #7
    Registered User
    Join Date
    06-26-2014
    Location
    Ontario,Canada
    MS-Off Ver
    Windows 7
    Posts
    4

    Re: Marco to Open all contents (.xlsm) in a folder and search each file for a word

    Troubleshoot would be in a merge cell/cells, C38:T41 or C42:T45 or C46:T49 or C50:T53.

    There are multiple sheets in each workbook. Sheet names are WO1, WO2 and WO3.

  8. #8
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Marco to Open all contents (.xlsm) in a folder and search each file for a word

    Ahh I must have been slow to respond, but mine is very similar to the above.

    AllFilesOpen.xlsb

  9. #9
    Registered User
    Join Date
    06-26-2014
    Location
    Ontario,Canada
    MS-Off Ver
    Windows 7
    Posts
    4

    Re: Marco to Open all contents (.xlsm) in a folder and search each file for a word

    Thanks a lot, I really appreciate the help.

    Works Great!

  10. #10
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Marco to Open all contents (.xlsm) in a folder and search each file for a word

    If it is solved can you mark it as such?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 05-21-2013, 08:22 AM
  2. Search a word file in a specific folder and copy text to excel
    By corinereyes in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-02-2012, 08:15 AM
  3. Macro to search folder including subfolders for file and open
    By kiraexiled in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-01-2012, 02:45 PM
  4. Replies: 2
    Last Post: 03-26-2012, 07:12 PM
  5. Replies: 7
    Last Post: 04-15-2006, 04:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1