Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

how to verify if an item exists in a array using excel vba?

Posted on 2013-07-01
13
532 Views
Last Modified: 2013-07-01
Hi,

I need a code to verify if an item exists in an array. Then if exists do nothing, if doesn't exists then add to the array. Any idea?
0
Comment
Question by:joyacv2
  • 7
  • 6
13 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39291934
You can use a collection instead, Keys in a collection must be unique and you get error 457 if you try to add a duplicate. So you wouldn't do it this way but here's an example.


Dim MyCollection As New Collection

On Error Resume Next
MyCollection.Add "value 1", "Key 1"
MyCollection.Add "value 2", "Key 1"
If Err.Number = 457 Then
    MsgBox "Key already in collevtion"
End If

Open in new window

MyCollection.Count will be 1 at the end of the code.
0
 
LVL 1

Author Comment

by:joyacv2
ID: 39291938
Hi,

How a collection works, how i refer to an item later? How I know the dimension of the collection? How is different from an array? Thank You
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39291943
Collections start at index 1 so this would return "Value 1" using the above code.

MsgBox MyCollection(1)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:joyacv2
ID: 39291945
But how is differs from an array?

My problem is that all my codes are in arrays
0
 
LVL 1

Author Comment

by:joyacv2
ID: 39291948
In the code why value 2 gives an error if it's not equal to value 1?
0
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39291953
Collection.Count is the number of items in the collection.

Besides the above which is the normal way of doing things there is also 'Before' and 'After' parameters that can be used if you like to insert values at a particular place in the collection.


This code would return "Value 2" followed by "Value 1" because the second Add tells VBA to insert the item before "Key 1"

Dim MyCollection As New Collection

On Error Resume Next
MyCollection.Add "value 1", "Key 1"
MyCollection.Add "value 2", "Key 2", "Key 1" 
If Err.Number = 457 Then
    MsgBox "Key already in collevtion"
End If

MsgBox MyCollection(1)
MsgBox MyCollection(2)

Open in new window


To insert after you do

MyCollection.Add "value 2", "Key 2", "Key 1" ,, "key value to insert after"
0
 
LVL 1

Author Closing Comment

by:joyacv2
ID: 39291956
EXCELLENT, THANK YOU FOR YOUR RESPONSE
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39291957
In the code why value 2 gives an error if it's not equal to value 1?
Because it checks the keys and not the values, so if you want to check for duplicate values make the key the same as the value. In other words

Dim MyCollection As New Collection

On Error Resume Next
MyCollection.Add "value 99", "value 99"
MyCollection.Add "value 1", "value 1"
MyCollection.Add "value 1", "value 1"
If Err.Number = 457 Then
    MsgBox "Key already in collection"
End If

Open in new window


In case it's not clear, the generalized code is

MyCollection.Add <some value>,<some key that must be a string>
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39291959
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39291962
BTW one more example. You don't have to use a key so this is perfectly valid and you wind up with 2 items in the collection.

Dim MyCollection As New Collection

On Error Resume Next

MyCollection.Add "value 1"
MyCollection.Add "value 1"

Open in new window

0
 
LVL 1

Author Comment

by:joyacv2
ID: 39291963
PERFECT, SO USING THE SAME SOLVE MY PROBLEM!
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 39291986
There's also a more advanced type of collection called a Dictionary but I don't have much experience with them.
0
 
LVL 1

Author Comment

by:joyacv2
ID: 39291991
for me, collection is new, i am working right with it, and works perfect!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question