+ Reply to Thread
Results 1 to 14 of 14

Check if Word Exists in Array?

  1. #1
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Check if Word Exists in Array?

    This should be a super-simple task, but I'm tearing my hair out due to Run-time Error 5, no matter what I try!
    I am trying to fill an array with individual words from a cell containing text. I already have code working that finds each word in succession. But before adding each new word to the array, I need to check that that word doesn't already exist within the array.
    I started off trying to use COUNTIF, but that only works on a range, not on an array.
    Latest iteration is:
    Please Login or Register  to view this content.
    where NewWord is a string variable containing the latest word, and WordArray is a variant array which will store the words.
    What am I doing wrong? Why does this give me a runtime error?
    I should note that when I get this error (on the very first word), NewWord is a string such as "flower" and WordArray is empty (having just been ReDim-ed as WordArray(6)).

  2. #2
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: Check if Word Exists in Array?

    Show me a sample wb, that will help a lot
    You actually can't use this method with an empty array

    Replace

    WorksheetFunction.Match

    to

    Application.Match
    Last edited by JEC.; 01-14-2022 at 04:19 PM.

  3. #3
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Check if Word Exists in Array?

    Why not use a Scripting.Dictionary, it's a lot easier to check if a value already exists.

  4. #4
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: Check if Word Exists in Array?

    That is very true, but not the easiest way to go if you don't know the dictionary

  5. #5
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Check if Word Exists in Array?

    I left off the final parenthesis in the code in my original post (sorry).
    I created a simplified file as a sample, and using
    Please Login or Register  to view this content.
    works in that file. But when I use it my original file, I still get the run-time error 5! Exact same statement yields two different results!

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Check if Word Exists in Array?

    Can you supply a sample workbook?

  7. #7
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: Check if Word Exists in Array?

    Could you post the file where it is not working?

  8. #8
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Check if Word Exists in Array?

    Here is the file. Gives runtime error 5 when it hits:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Check if Word Exists in Array?

    And here, for reference, is a simplified file with the exact same statement, that does NOT generate the runtime error.
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Check if Word Exists in Array?

    There is no AWordArray but WordArray has been erased by Erase AWordArray
    Last edited by AB33; 01-14-2022 at 06:03 PM.

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

    Re: Check if Word Exists in Array?

    I have not yet looked at your attachments.
    In the meantime you can check if you can use this.

    Sixthsense's solution
    A1 has the sentence (=String).
    Column F (= Column 6) has the words your checking against the array.
    Please Login or Register  to view this content.

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

    Re: Check if Word Exists in Array?

    And here is an attachment to play with.
    Attached Files Attached Files

  13. #13
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Check if Word Exists in Array?

    Quote Originally Posted by AB33 View Post
    There is no WordArray but WordArray has been erased by Erase AWordArray
    Ah, of course!!
    Forgot that Erase doesn't merely clear a dynamic array. I guess I should Redim each time rather than Erase!
    THANKS for locating my screw-up!

  14. #14
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,377

    Re: Check if Word Exists in Array?

    If you are interested, with the dictionary.
    List some items in column A to test

    Please Login or Register  to view this content.

+ 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 CHECK FILE.NAME HAS EXISTS OR NOT IN FOLDER,if not exists then copy
    By daboho in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-13-2018, 03:50 AM
  2. VBA Check of string exists in an Array
    By mchilapur in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-04-2016, 10:30 AM
  3. VBA to check if "same word" exists in a cell
    By puuts in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-30-2015, 03:11 AM
  4. Check cell for word, If word exists in cell, assign that cell to corresponding category.
    By Trevasaurus in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-12-2012, 12:35 PM
  5. Check If Value Exists In Array Help
    By mccrimmon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-26-2011, 12:00 PM
  6. Check if word exists and count how many times, help
    By execine in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-07-2010, 02:08 PM
  7. How to check whether a value exists in a multidimensional array variable?
    By stevenf in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-30-2009, 01:07 PM

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