+ Reply to Thread
Results 1 to 13 of 13

Testing for existence of value in array throws an error

  1. #1
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Testing for existence of value in array throws an error

    While adding new values to an array I'd like to check with each new value if it already exists in the array.
    The following however throws an error on this line
    Please Login or Register  to view this content.
    invalid procedure call or argument
    Please Login or Register  to view this content.
    Link to file
    Last edited by Jonathan78; 08-25-2017 at 04:03 AM. Reason: Link to file added

  2. #2
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Testing for existence of value in array throws an error

    I think you have to ReDim the array (as 2-dimensional??) BEFORE using it with Match:
    Please Login or Register  to view this content.
    Clicking the Add Reputation star below helpful posts is a great way to show your appreciation.
    Please mark your threads as SOLVED upon conclusion (Thread Tools above Post # 1). - Lee

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,358

    Re: Testing for existence of value in array throws an error

    Hi Jonathan,

    If you have an array of numbers in Excel from say B2 to N7, another method to check if there are duplicates would be to use the frequency formula

    =Max(Frequency(B2:N7,B2:N7)

    If this is over 1 then there is a duplicate somewhere in your array of numbers.

    You could also use Conditional Formatting and set it to find duplicates.

    If you want to do it using VBA the above might be usless information.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Testing for existence of value in array throws an error

    On the first iteration of the loop the array will be empty, not sure you can check for a value in an empty array.
    If posting code please use code tags, see here.

  5. #5
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Re: Testing for existence of value in array throws an error

    None of the above solved the problem.
    I added the file to my post as an example.

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Testing for existence of value in array throws an error

    Why not use a Dictionary ? Much easier to create unique list.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  7. #7
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Re: Testing for existence of value in array throws an error

    I need the array to perform tasks somewhere else in the code

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Testing for existence of value in array throws an error

    Like this.

    Please Login or Register  to view this content.
    Last edited by bakerman2; 08-26-2017 at 06:49 AM.

  9. #9
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Re: Testing for existence of value in array throws an error

    Thanks bakerman2 I will give it a try once I am at my computer again.

    Can anyone tell me why my code didn't work?

    And is it possible to get it work with perhaps a small adjustment?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Testing for existence of value in array throws an error

    The reason you get the error you describe is because the array is empty, and undimensioned, on the first iteration of the loop, just as I suggested in my original post

  11. #11
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585
    Quote Originally Posted by Norie View Post
    The reason you get the error you describe is because the array is empty, and undimensioned, on the first iteration of the loop, just as I suggested in my original post

    Thanks for explaining that Norie.
    How do I overcome this?

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Testing for existence of value in array throws an error

    On the first iteration of the loop you don't need to check if the value you are trying to add to the array is already in the array.

    So you could either skip the code to check for a duplicate on the first iteration or, and perhaps better, populate the array with the first value in the range before you start the loop.

    That second option might look something like this.
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    08-19-2009
    Location
    Netherlands, Amsterdam
    MS-Off Ver
    Excel 2010
    Posts
    585

    Re: Testing for existence of value in array throws an error

    That worked, Thank!

+ 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. Code Issues: Throws error everytime I add a row
    By jaredmccullough in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-08-2014, 11:52 AM
  2. Error when testing for existence of shape object
    By AlvaroSiza in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-20-2012, 09:13 PM
  3. How to check for existence of PageField and what is error 1004
    By Thamizh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-25-2011, 09:31 AM
  4. Simple If statement throws an error
    By ronanm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-01-2011, 10:07 AM
  5. workbook.open throws 1004 error
    By gary.smith@primeexalia.com in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-03-2006, 06:45 PM
  6. How do I perform a certain function if VBA throws up an error?
    By Matt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-24-2006, 11:10 AM
  7. testing the existence of a formula in a cell
    By jérome Yacc in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-07-2005, 12:23 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