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":
Now I know the value of the last used row, I write the contents of column A into an array called SRarray:
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:
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:
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.
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.