LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 77
Default comparing elements from 2 arrays using VBA code

Just another idea. I think you are working with only numbers.
... which numbers are in the first array but not the second.


This returns the Complement, those that are in A, and not in B. (4,11,12 &
13)
I'm not sure how fast this would be with large arrays though.

Sub Complement()
Dim i As Long
Dim t As Long
Dim p As Long

Dim v1 As Variant
Dim v2 As Variant
Dim v3 As Variant

v1 = Array(4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
v2 = Array(5, 6, 7, 8, 9, 10)
ReDim v3(0 To UBound(v1))

p = 0
On Error Resume Next
With WorksheetFunction
For i = LBound(v1) To UBound(v1)
t = .Match(v1(i), v2, 0)
If t = 0 Then
v3(p) = v1(i)
p = p + 1
Else
t = 0
End If
Next i
End With
ReDim Preserve v3(1 To p)
End Sub

--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"lopsided" wrote in message
...
Hi,
I am having a bit of trouble with arrays. I have created two list
arrays which are of different sizes and have some of the same numbers
in them. I want to go through one list and compare every element within
it with every element on the second list. I need to find out
(basically) which numbers are in the first array but not the second.

The outline of the code i am using is as follows:

i = 1
j = 1
For i = LBound(array1) To UBound(array1)
Do While array1(i) < array2(j)
For j = LBound(array2) To UBound(array2)
If j = UBound(array2) Then
~~~Return Some Values~~~
GoTo GetOut
Else: End If
Next j
Loop
GetOut:
Next i

The problem lies in that although there are no bugs in my code the
macro is returning all the data (as if the lists had nothing in
common).

Hope someone can help me!!

Thanks a lot
Tom



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing ranges/arrays asaylor Excel Worksheet Functions 7 June 13th 06 11:53 PM
Elements and Arrays in Excel Lighthouseman Excel Worksheet Functions 6 February 1st 06 10:06 AM
Comparing 2 arrays [email protected] Excel Worksheet Functions 3 January 2nd 06 02:31 AM
Comparing Arrays TangentMemory Excel Discussion (Misc queries) 2 May 13th 05 05:06 PM
Comparing Arrays KL Excel Worksheet Functions 9 December 3rd 04 08:58 PM


All times are GMT +1. The time now is 05:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"