Hello everyone
I need your help with my code. I get runtime error #9 (subscript out of range). I have a function to check if a value is in an array or not. I can do this with other ways but I need to understand what is wrong with my writing. I hope you can help me answer that
'setting up arrays
Dim New_Cons(), A(), B(), Old_Cons(), Memberships() As Variant, I As Integer
ReDim Preserve Old_Cons(0 To sht2.Cells(Cells.Rows.Count, "O").End(xlUp).Row)
ReDim Preserve New_Cons(0 To sht1.Cells(Cells.Rows.Count, "B").End(xlUp).Row - 11)
Old_Cons() = sht2.Range("O2:O" & sht2.Cells(Cells.Rows.Count, "o").End(xlUp).Row).Value
New_Cons() = sht1.Range("b11:B" & sht1.Cells(Cells.Rows.Count, "B").End(xlUp).Row).Value
'comparing arrays
For I = 0 To UBound(Old_Cons)
If IsInArray(Old_Cons(I), New_Cons) = False Then
ReDim Preserve Memberships(0 To K)
Memberships(K) = Old_Cons(I) & "/" & "Drop"
K = K + 1
End If
Next I
My function is as below:
Public Function IsInArray(A As Variant, B As Variant) As Boolean
Dim I As Integer, X As Integer
IsInArray = True
On Error GoTo NotInArray
X = WorksheetFunction.Match(A, B, 0)
If Not IsNumeric(X) Then
IsInArray = False
End If
NotInArray:
End Function
I need your help with my code. I get runtime error #9 (subscript out of range). I have a function to check if a value is in an array or not. I can do this with other ways but I need to understand what is wrong with my writing. I hope you can help me answer that
'setting up arrays
Dim New_Cons(), A(), B(), Old_Cons(), Memberships() As Variant, I As Integer
ReDim Preserve Old_Cons(0 To sht2.Cells(Cells.Rows.Count, "O").End(xlUp).Row)
ReDim Preserve New_Cons(0 To sht1.Cells(Cells.Rows.Count, "B").End(xlUp).Row - 11)
Old_Cons() = sht2.Range("O2:O" & sht2.Cells(Cells.Rows.Count, "o").End(xlUp).Row).Value
New_Cons() = sht1.Range("b11:B" & sht1.Cells(Cells.Rows.Count, "B").End(xlUp).Row).Value
'comparing arrays
For I = 0 To UBound(Old_Cons)
If IsInArray(Old_Cons(I), New_Cons) = False Then
ReDim Preserve Memberships(0 To K)
Memberships(K) = Old_Cons(I) & "/" & "Drop"
K = K + 1
End If
Next I
My function is as below:
Public Function IsInArray(A As Variant, B As Variant) As Boolean
Dim I As Integer, X As Integer
IsInArray = True
On Error GoTo NotInArray
X = WorksheetFunction.Match(A, B, 0)
If Not IsNumeric(X) Then
IsInArray = False
End If
NotInArray:
End Function