MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 30th, 2004, 08:16 AM   #1
peioz
 
Join Date: Jul 2002
Posts: 78
Default compare 2 vba arrays

Is there a more elegant way of comparing 2 array than is shown below ?
I just want to check if the 2 arrays are exactly identical......

for i = 1 to 10

if array1(i)=array2(i) then.......

next i

TIA....
peioz is offline   Reply With Quote
Old Mar 30th, 2004, 08:27 AM   #2
Andrew Poulsom
MrExcel MVP
 
Andrew Poulsom's Avatar
 
Join Date: Jul 2002
Posts: 32,030
Default Re: compare 2 vba arrays

I don't know about more elegant, but it would be quicker to test for inequality and exit the loop when they don't match.

Code:
x = True
For i = LBound(array1) to UBound(array1)
   If array1(i) <> array2(i) Then
      x = False
      Exit For
   End If
Next i
If x = True Then
   MsgBox "Match"
Else
   MsgBox "Item " & i & " does not match"
End If
Andrew Poulsom is offline   Reply With Quote
Old Mar 30th, 2004, 08:31 AM   #3
peioz
 
Join Date: Jul 2002
Posts: 78
Default Re: compare 2 vba arrays

Yours look more robust to me.... I thought there would be some kind of match commando ...quess not.

Thanks !
peioz is offline   Reply With Quote
Old Mar 30th, 2004, 08:53 AM   #4
Andrew Poulsom
MrExcel MVP
 
Andrew Poulsom's Avatar
 
Join Date: Jul 2002
Posts: 32,030
Default Re: compare 2 vba arrays

If you don't need to know which item mismatches you could use the Join function (Excel 2000 and above):

Code:
If Join(array1, "") = Join(array2, "") Then
   MsgBox "Match"
Else
   MsgBox "Mismatch"
End If
Andrew Poulsom is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 05:53 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.