compare 2 vba arrays

peioz

Board Regular
Joined
Jul 17, 2002
Messages
78
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....
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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
 
Upvote 0
Yours look more robust to me.... I thought there would be some kind of match commando ...quess not.

Thanks !
 
Upvote 0
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
 
Upvote 0
hello guys... here i hav a small prb relatd to string arrays.
i have two string arrays as string1=F1,F2,F4,F16,F3
and string2= F2,F11,F7,F8,F12,F3.F21 (these two arrys need not be of same size)
plz suggest me a code to VBE(visual basic editor).
make it asap
thanks....!!!:)
 
Upvote 0
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

I would reccomend to include such separator string (for example, "|" symbol) in Join function that prevents wrong answer.
 
Upvote 0

Forum statistics

Threads
1,215,608
Messages
6,125,820
Members
449,265
Latest member
TomasTeix

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top