Solved

create a common array

Posted on 2002-04-22
4
246 Views
Last Modified: 2010-05-02
Hi,

I have 2 arrays : arr1(5) , arr2(3) . How can I create a third array which contains only the common items in arr1 & arr2
Important: arr2() may becomes one time longer than arr1() , I mean that their long are not fixed
I look forward the code which can do that.

Thanx
0
Comment
Question by:ray14
  • 2
4 Comments
 
LVL 3

Expert Comment

by:jjmartin
ID: 6961397
Are the array's sorted in any particular order?
0
 
LVL 15

Accepted Solution

by:
ameba earned 100 total points
ID: 6961438
' Form1 code
Option Explicit

Private Sub Form_Click()
    Print Join(GetCommon(Split("a b cc dd", " "), Split("cc dd e f", " ")), " ")
End Sub

' returns items which are in a1 and a2
Private Function GetCommon(a1 As Variant, a2 As Variant) As Variant
    Dim col1 As New Collection, colBoth As New Collection, arrBoth() As String
    Dim i As Long, tmp As String
    On Error Resume Next
   
    ' build collection of unique values
    For i = 0 To UBound(a1)
        col1.Add a1(i), "k" & a1(i)
    Next
   
    ' test all values in a2
    For i = 0 To UBound(a2)
        Err.Clear
        tmp = col1.Item("k" & a2(i)) ' if Item does not exist, we'll get error
        If (Err = 0) Then colBoth.Add a2(i), "k" & a2(i) 'Item exists
    Next
   
    ' fill array
    ReDim arrBoth(0 To colBoth.Count - 1)
    For i = 1 To colBoth.Count
        arrBoth(i - 1) = colBoth.Item(i)
    Next
    GetCommon = arrBoth    ' return array
End Function
0
 

Author Comment

by:ray14
ID: 6963434
Hi ameba,

Thanks for your answer and it is  nice to receive your help again. As I promised in my last question I’ll increase the points by +50 then it will be 100  thanks again.

 Ray14
0
 
LVL 15

Expert Comment

by:ameba
ID: 6963463
Thank you  :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now