+ Reply to Thread
Results 1 to 7 of 7

VBA to search files within a folder for a specific string and return file name

  1. #1
    Registered User
    Join Date
    06-03-2014
    Posts
    3

    VBA to search files within a folder for a specific string and return file name

    Hi everyone,

    First time on a forum.

    I'm fairly new to excel vba and I'm currently using 2010.

    I'm looking for a code in Excel vba that will:
    - Take a string from a cell A1 i.e. "hello" and search a folder with 20 word files for that string.
    - The output will be a list of file names (rows B1 to B..) in which "hello" appears at least once.

    Is this possible? I've research into Dir and wildcard functions but I'm too much of a novice to even begin.

    Any help would be greatly appreciated.

    Thanks.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,912

    Re: VBA to search files within a folder for a specific string and return file name

    Welcome to the forum.

    I assumed that you meant to find the word in the filename only - searching the contents of the file is a different task: not difficult, just different.

    Copy this macro into a standard codemodule in a macro-enabled workbook, save the workbook with this macro in the folder of interest, type the word in cell A1 and run the macro.

    Please Login or Register  to view this content.
    Last edited by Bernie Deitrick; 06-03-2014 at 01:30 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    06-03-2014
    Posts
    3

    Talking Re: VBA to search files within a folder for a specific string and return file name

    Bernie,

    I really appreciate your response.

    I actually need to search through the contents of the files.

    You say this if different, not difficult...hmmm haha.

    Cheers again,

    Tim

  4. #4
    Registered User
    Join Date
    06-03-2014
    Posts
    3

    Re: VBA to search files within a folder for a specific string and return file name

    Bernie,

    I also want a command button to activate the macro (i.e. a search button).

    The folder location to search in is: P:\023 Capability Management System (CMS).

    Apologies for piling it on.

  5. #5
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: VBA to search files within a folder for a specific string and return file name

    Hi..

    This works.. I haven't really done much at all concerning Word.. but it seems to do the trick..

    Change the Filepath and Sheet names to suit..

    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    09-06-2011
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: VBA to search files within a folder for a specific string and return file name

    Many thanks - I was just about to ask a very similar question.

    Can the solution(s) be adapted:

    1) to allow subfolders to be searched
    2) for the file path results in the Excel worksheet to be produced as hyperlinks that allow the relevant file to be opened?

    Thanks.

  7. #7
    Registered User
    Join Date
    09-06-2011
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: VBA to search files within a folder for a specific string and return file name

    Quote Originally Posted by 1819 View Post
    Many thanks - I was just about to ask a very similar question.

    Can the solution(s) be adapted:

    1) to allow subfolders to be searched
    2) for the file path results in the Excel worksheet to be produced as hyperlinks that allow the relevant file to be opened?

    Thanks.
    Apologies - it already does subfolders.

+ 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. Search for a string on all excel files inside a ZIp folder
    By Moreno20 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2014, 03:45 PM
  2. Want to search for string in list of files in a folder
    By DeepthiReddy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-27-2012, 03:21 AM
  3. Case insensitive search for string within folder of excel files
    By vba_novice in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-22-2011, 11:21 PM
  4. search a folder and return the most recent file's name
    By Pink_Mtl in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 05-07-2010, 04:13 PM
  5. Search files in folder based on search string
    By djvice in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-22-2008, 01:12 AM

Tags for this Thread

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