Need help comparing two arrays

pete212

New Member
Joined
Aug 20, 2012
Messages
20
Hello,

I have 2 excel workbooks, one called "system" and the other called "bond". Essentially, I read data from each workbook into 2 separate arrays and then want to compare the two. I'll walk you through my code step by step and hopefully it will make sense:

The data that I need is in Column A, starting row 2. The total number of rows is dynamic, so first I find the last used row using the below and then write the number of the last used row into a variable called "SystemNextFree":

Code:
Dim xLastRow As Long
   With Application.ActiveSheet
       xLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

SystemNextFree = xLastRow

Now I know the value of the last used row, I write the contents of column A into an array called SRarray:

Code:
Dim i As Integer
Dim c As Integer
ReDim SRarray(SystemNextFree) As String
c = 0

For i = 2 To SystemNextFree

SRarray(c) = Cells(i, "A").Value
Cells(i, "N").Value = SRarray(c)   ' This line is only for debugging purposes so I can check the array is populated correctly
c = c + 1

Next i

This works perfectly as proved by looking at column N. Next I open the second workbook (bond.xls) and essentially repeat the process writing the data to a second array, this time called Barray:

Code:
Dim bond As Excel.Workbook
Set bond = Workbooks.Open("C:\bond.xls")

bond.Sheets("All").Select
Range("A1").Select


Dim xLastRow As Long
   With Application.ActiveSheet
       xLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

 BondNextFree = xLastRow

Dim i2 As Integer
Dim c2 As Integer
Dim x2 As Integer
ReDim Barray(BondNextFree) As String
c2 = 0
x2 = 0

For i2 = 2 To BondNextFree
Barray(c2) = Cells(i2, "A").Value
Cells(i2, "N").Value = Barray(c2) ' This line is only for debugging purposes so I can check the array is populated correctly

    
c2 = c2 + 1
Next i2

Again, this works all fine as proved by column N. Now I want to do is compare the two arrays, and if the data from the bond sheet exists in the system sheet, it writes "EXISTING" to column O and if it does NOT exist in the system sheet, it writes "New" to column O. I'm trying to do this by amending the above section of code with the below (new code in blue text) but does not work:

Code:
Dim bond As Excel.Workbook
Set bond = Workbooks.Open("C:\bond.xls")

bond.Sheets("All").Select
Range("A1").Select


Dim xLastRow As Long
   With Application.ActiveSheet
       xLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

 BondNextFree = xLastRow

Dim i2 As Integer
Dim c2 As Integer
Dim x2 As Integer
ReDim Barray(BondNextFree) As String
c2 = 0
x2 = 0

For i2 = 2 To BondNextFree
Barray(c2) = Cells(i2, "A").Value
Cells(i2, "N").Value = Barray(c2)

    [COLOR=#0000cd]For x2 = 0 To SystemNextFree
    If Cells(i2, "N").Value = [/COLOR][COLOR=#ff0000]SRaaray[/COLOR][COLOR=#0000cd](x2) Then
    Cells(i2, "O").Value = "EXISTING"
    Else
    Cells(i2, "O").Value = "NEW"
    Next x2
    End If[/COLOR]

    
c2 = c2 + 1
Next i2

It errors out on the red text saying "Sub or function is not defined". I'm *guessing* based on that error it means it's not remembering my SRarray array that was created earlier in the code for some reason. All of this code exists within the same sub.

Any ideas how I can get this working? Note that the EXISTING / NEW statements written to column O is only for debugging purposes at the moment. Once I get this working these will be removed and replaced with a bit more work, mainly copying the new data from the bond workbook to the system workbook.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Firstly, you have a typo, check the spelling of the value in red. ;)
Secondly you do not need to use a loop to populate the array, you can do it like
Code:
Dim SRarray As Variant
SRarray = Range("A2:A" & xLastrow).Value
 
Upvote 0
Thanks Fluff,

That looks like it would be way more efficient. How would I go about writing the contents of the variant to check that its populated correctly? If I do "Cells(1, "N").Value = SRarray" then it only prints the first value (cell A2). And if I try "Cells(1, "N").Value = SRarray(1)" for example it errors out saying subscript is out of range.
 
Upvote 0
Further to the above, you are not actually comparing the values in the 2 arrays, you are comparing the value in SR array to cell values.
How many rows of data are you normally looking at?

EDIT:
Is the macro in the "System" workbook?
 
Last edited:
Upvote 0
try this
Code:
Sub CheckIfExists()
   Dim Wbk As Workbook
   Dim Sws As Worksheet, Bws As Worksheet
   Dim Sary As Variant, Bary As Variant, Oary As Variant
   Dim i As Long
   
   Set Sws = ActiveSheet
   Set Wbk = Workbooks.Open("C:\MrExcel\Book1.xlsm")
   Set Bws = Wbk.Sheets("All")
   Sary = Sws.Range("A2", Sws.Range("A" & Rows.count).End(xlUp))
   Bary = Bws.Range("A2", Bws.Range("A" & Rows.count).End(xlUp))
   ReDim Oary(1 To UBound(Bary), 1 To 1)

   With CreateObject("scripting.dictionary")
      For i = 1 To UBound(Sary)
         .Item(Sary(i, 1)) = Empty
      Next i
      For i = 1 To UBound(Bary)
         Oary(i, 1) = IIf(.exists(Bary(i, 1)), "Existing", "New")
      Next i
   End With
   Bws.Range("N2").Resize(UBound(Oary)).Value = Application.Index(Oary, , 1)
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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