+ Reply to Thread
Results 1 to 5 of 5

Display Message Box When cell(s) contain a certain text in a column

  1. #1
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Display Message Box When cell(s) contain a certain text in a column

    Greetings everyone,
    I am trying to display a message box when a workbook opens if column B contains the word "term". With the use of the below code; however, I am getting a Run-time error '1004' Method 'Range' of object' 'Global' failed. I would appreciate if someone can tell me what I am doing wrong.
    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Display Message Box When cell(s) contain a certain text in a column

    You need to specify which sheet the range object refers to, e.g.

    Please Login or Register  to view this content.
    Also, there's no point doing an intersect with Target - there isn't a range called Target in this sub, so the intersection will always return nothing.

    Also, also, is rng meant to refer to a single cell (Range("B2:B2")) or a whole column (Range("B:B"))? What you've got at the moment is an invalid combination of the two. If it's meant to be multiple cells then you can't evaluate all of the values using rng = "term", you'll have to use a .Find.

  3. #3
    Forum Contributor
    Join Date
    02-20-2011
    Location
    Candada
    MS-Off Ver
    Microsoft Office 365
    Posts
    174

    Re: Display Message Box When cell(s) contain a certain text in a column

    Thank you Mr. Andrew-R for your feedback. this is what I have done and so far it works. However, is there a way I can add to display the cell location where the word "term" appears? Here is my code
    Please Login or Register  to view this content.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Display Message Box When cell(s) contain a certain text in a column

    Hi,

    I don't know why you think the code is working as I think you expect it to work since as Andrew has intimated you will always see the message box because you are not testing or searching for anything.

    I a situation like this I always prefer to use a cell on the worksheet which evaluates the condition you are testing for, and then if necessary use a macro to report the value of the cell.

    So for instance if you are looking for the occurrence of the word 'term' in column B then in a cell say A1 enter

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    this will return the cell address of the first occurrence of 'term'. You could conditionally format this cell so that it stands out with say a red background if it's not equal to 'Term not found', which may be sufficient for you. If you really need a macro then use something like

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Display Message Box When cell(s) contain a certain text in a column

    You can modify your code slightly to display the first cell containing "term":

    Please Login or Register  to view this content.
    Changing it to report multiple matches will involve looping through all of the matches and might produce a very long message box.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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