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

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/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default comparing elements from 2 arrays using VBA code

Thanks for your help it seems like your method is more powerful... but i
think the problem i may have been having is that for some reason on
array element would be displayed as "24" and another as 24 and the
coding then said they are different.

But ive sorted that now
Thanks for your help!



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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default comparing elements from 2 arrays using VBA code

24 is different from "24"

In some cases, excel will coerce the comparison to the same data type and
the results would be true. I assume there are other cases where it won't.

--
Regards,
Tom Ogilvy



"lopsided" wrote in message
...
Thanks for your help it seems like your method is more powerful... but i
think the problem i may have been having is that for some reason on
array element would be displayed as "24" and another as 24 and the
coding then said they are different.

But ive sorted that now
Thanks for your help!



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



  #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/



Reply
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 02:46 PM.

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

About Us

"It's about Microsoft Excel"