I am trying to create a loop in my code that checks a list of arrays (with strings stored in them) against the 'Circs' string variable (that is user-defined) and returns a value of true if the string exists in any of the arrays.
Have used Debug and other SO answers to try and understand what is going on. Tried using the Array() feature where CircsG1 = Array()...... which just causes the error to appear on the line For j = 1 To UBound(ListofArrays(i)) instead.
Private Sub ComboBox2_Change()
''When S&S Circumstance selection is made
Dim WS As Worksheet
Set WS = ThisWorkbook.Sheets("Administration")
''Arrays where strings are stored
Dim CircsG1 As Variant
Dim CircsG2 As Variant
Dim CircsG3 As Variant
''Array of Array
Dim ListofArrays As Variant
''String to compare against
Dim Circs As String
Dim i As Integer
Dim j As Integer
Dim NoOfArr As Integer
NoOfArr = 3
CircsG1 = WS.Range("P2:P19").Value
CircsG2 = WS.Range("Q2:Q7").Value
CircsG3 = WS.Range("R2:R5").Value
ListofArrays = Array(CircsG1, CircsG2, CircsG3)
Circs = Me.ComboBox2.Value
For i = 1 To NoOfArr
For j = 1 To UBound(ListofArrays(i))
If InStr(ListofArrays(i, j), Circs) Then
MsgBox ListofArrays(i)
''and some other code
Else
Exit Sub
End If
Next
Next
Skip:
End Sub
Having investigated my code using various other answers on SO, there are a couple of things I have identified.
The UBound(ListofArrays(i)) is returning a value that is less than the number of strings stored in the array it is looking at. i.e. the range I defined means this value should be 17 but it returning a value of 6.
Runtime error 9 ,Subscript error on line
If InStr(ListofArrays(i, j), Circs) Then