+ Reply to Thread
Results 1 to 11 of 11

How to check if a file contains a string?

  1. #1
    Forum Contributor
    Join Date
    08-20-2009
    Location
    new york
    MS-Off Ver
    Excel 2003
    Posts
    119

    How to check if a file contains a string?

    Basically, I want to perform "Find ... within Workbook".

    When I write the VBA code, do I have to loop through each sheet?

    Thanks.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to check if a file contains a string?

    In VBA, yes.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to check if a file contains a string?

    Hello niuyuer,

    Do you want to find only the first occurrence of the string on a worksheet or all of occurrences on all sheets?
    Are you searching only the cells' values, formulas, or both?
    What do you want to do once a string is found?

    The answers to these questions impact how your VBA code needs to be written.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Forum Contributor
    Join Date
    08-20-2009
    Location
    new york
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: How to check if a file contains a string?

    Quote Originally Posted by Leith Ross View Post
    Hello niuyuer,

    Do you want to find only the first occurrence of the string on a worksheet or all of occurrences on all sheets?
    Are you searching only the cells' values, formulas, or both?
    What do you want to do once a string is found?

    The answers to these questions impact how your VBA code needs to be written.
    Thanks.

    Search cell's values. Actually, search some cells' value in file A, and see if File B contains those values. To me, it does not matter it is first occurrence or not, all I want to know is if it is included in File B.

    But the difficulty is: for example, I have value "ABC" in File A, and would like to know if file B contains ABC. I can search file B, the issue is if File B contains "ABC*", the code will consider that File B contains "ABC". This is not what I want. Let me be specific, let us say file B contains (XYZ + ABCD)/2 > 0, the answer I like to get is "File B does not contain ABC", since ABCD is different from ABC. This is the difficult part to turn into code, I know how to write the code to show (XYZ + ABCD)/2 > 0 containing ABC, but this is just not accurate.

  5. #5
    Forum Contributor
    Join Date
    08-20-2009
    Location
    new york
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: How to check if a file contains a string?

    Actually, I have the rule to determine, if I want to know if file B contains ABC, and if I find ABC* in file B and the character immediately following C is also a letter (what I mean letter is: A, B, C, ...., X, Y, Z), then this is not what I am looking for.

    So how can I know if a particular character in a string is a letter or a number? There should be the only two cases, the string I want to search can be either letter only, or letter & number mixed, not other symbols.

    Here are desire output results:

    If search ABC: ABCF does not contain ABC, ABC4 does not contains ABC.

    If search A20: A205 does not contain A20.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to check if a file contains a string?

    Hello niuyeur,

    Here are two macros that work together to find search terms in workbook "A" in workbook "B". The search terms in workbook "A" are pulled from the cell values in column "A" starting with cell "A1" on "Sheet1".

    Each sheet in workbook "B" is searched for each term from workbook "A". When found, a message box appears informing you which term was found and the name of the workbook it was found in.

    Add a new standard Module to your VBA project. Copy and paste this code into it. Put your search terms in column "A" of "Sheet1". Run the macro FindInWorkbook. All open workbooks, except the workbook with the macro, will be searched.

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    08-20-2009
    Location
    new york
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: How to check if a file contains a string?

    Quote Originally Posted by Leith Ross View Post
    Hello niuyeur,

    Here are two macros that work together to find search terms in workbook "A" in workbook "B". The search terms in workbook "A" are pulled from the cell values in column "A" starting with cell "A1" on "Sheet1".

    Each sheet in workbook "B" is searched for each term from workbook "A". When found, a message box appears informing you which term was found and the name of the workbook it was found in.

    Add a new standard Module to your VBA project. Copy and paste this code into it. Put your search terms in column "A" of "Sheet1". Run the macro FindInWorkbook. All open workbooks, except the workbook with the macro, will be searched.

    Please Login or Register  to view this content.
    Thanks much, actually, I can write on my own.

  8. #8
    Forum Contributor
    Join Date
    08-20-2009
    Location
    new york
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: How to check if a file contains a string?

    Let us say, cell A1 is "ABC", cell A2 is "GABCD DEF ABC".

    Cell A2 contains two ABC, first ABC starts at position 2, second ABC starts at position 11.

    So how should I write VBA to get the numbers 2 and 11?

    Thanks.

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: How to check if a file contains a string?

    niuyuer, it's not necessary to quote an entire post to respond to it. It takes up a lot of space in the thread. If you need to reply to something specific then you can quote just that part. If your post comes immediately after the post you're responding to, it will be obvious what your post is about.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: How to check if a file contains a string?

    Hello niuyeur,

    You will need to use the InStr function to return the starting position of a string within another string.

  11. #11
    Forum Contributor
    Join Date
    08-20-2009
    Location
    new york
    MS-Off Ver
    Excel 2003
    Posts
    119

    Re: How to check if a file contains a string?

    Thank you so much.

+ 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. How to check if excel file is Checked Out in SharePoing Check Out, run code, Check In
    By jrtraylor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-08-2016, 04:36 PM
  2. Replies: 7
    Last Post: 04-15-2015, 09:32 AM
  3. [SOLVED] Function to check if string is valid/legal windows file name?
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-29-2013, 12:42 AM
  4. [SOLVED] procedure to check if text string is a valid worksheet name before adding it to file
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-23-2013, 03:10 PM
  5. Replies: 2
    Last Post: 12-12-2012, 04:19 AM
  6. Replies: 8
    Last Post: 09-27-2012, 10:46 PM
  7. Replies: 1
    Last Post: 08-04-2009, 06:42 AM

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