I am creating a code that compares new and old lists in order to find items that exists in column B but not in column E and vice versa. I do this for multiple lists.
In Excel I use the function in column A and drag down
=IF(ISNA(VLOOKUP(B4,$E$4:$E$65537,1,FALSE)),"0","1")
Where B contains an identifier for the old list, C contains a name for the identifier for the old list, and E contains the identifier for the new list.
Option Explicit
Option Base 0
' **** Declaring variables ****
' Worksheets and workbooks
Public ws_C As Worksheet
Public wkb As Workbook
' Integers
Public lr_pos_old As Integer
Public lr_pos_new As Integer
Public lr_neg_old As Integer
Public lr_neg_new As Integer
Public oldColumn As Integer
Public newColumn As Integer
Public StartRow As Integer
Public i As Integer
Public j As Integer
Public colSpace As Integer
' Arrays
Public ListArrOld As Variant
Public ListArrNew As Variant
Sub main()
' This sub sets up general declarations and options
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set wkb = ThisWorkbook
Set ws_C = wkb.Sheets("Comparison")
StartRow = 4
colSpace = 6
oldColumn = 2
newColumn = 5
lr_pos_old = ws_C.Range("C12").End(xlDown).Row ' Lastrow for old positive list
lr_pos_new = ws_C.Range("F12").End(xlDown).Row ' Lastrow for new positive list
lr_neg_old = ws_C.Range("I12").End(xlDown).Row ' Lastrow for old negative list
lr_neg_new = ws_C.Range("L12").End(xlDown).Row ' Lastrow for new negative list
ListArrOld = Array(lr_pos_old, lr_neg_old)
ListArrNew = Array(lr_pos_new, lr_neg_new)
' Calling subs
Call CompareLists
Application.StatusBar = False
ws_C.Activate
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Sub CompareLists()
' This sub compares the positive and negative lists from the old and new boardmeeting report
Application.StatusBar = "Comparing new and old lists ..."
' Comparing old vs new list: Value 1 if included in the new list
With Application.WorksheetFunction
For j = LBound(ListArrOld) To UBound(ListArrOld)
For i = StartRow To ListArrOld(j)
ws_C.Cells(i, 1 + j * colSpace) = _
.If(.IsNA(.VLookup(ws_C.Cells(i, oldColumn + j * colSpace), _
ws_C.Range(ws_C.Cells(StartRow, newColumn + j * colSpace), ws_C.Cells(ListArrNew(j), newColumn + j * colSpace)), 1, False)), "0", "1")
Next i ' Next row
Next j ' Next list
End With
' Comparing new vs old: Value 1 if included in the old list
' Similar code
End Sub
I expect column A to get values of 0 and 1 but instead my code fails at
ws_C.Cells(i, 1 + j * colSpace) = _
.If(.IsNA(.VLookup(ws_C.Cells(i, oldColumn + j * colSpace), _
ws_C.Range(ws_C.Cells(StartRow, newColumn + j * colSpace), ws_C.Cells(ListArrNew(j), newColumn + j * colSpace)), 1, False)), "0", "1")
with the
run-time error 438 "object does not support this property or method".