I am getting subscript out of range error 9 on my 2nd lineof the For loop below.
i = 7 (row number), and c1 = 59, c2=60 (column numbers). Why am I not getting subscript out of range here?
Should I be using 1 or 2 instead of c1 or c2? When I use 1 or 2 it is not picking up my columns correctly.
Dim c1 As Variant
Dim c2 As Variant
Dim arr() as Variant
Set rw = ws.Rows(6)
lastR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
c1 = Application.Match("Col 59)", rw, 0)
c2 = Application.Match("Col 60", rw, 0)
If Not IsError(c1) And Not IsError(c2) Then 'found both column headers?
arr = ws.Range(rw.Cells(c1), rw.Cells(c2)).Resize(lastR).Value2
Else
MsgBox "One or both required column headers not found!"
End If
'Loop to find Empty and Non empty fields
For i = 7 To UBound(arr) 'Row 7 is the row the data starts
If (arr(i, c1) <> "" And arr(i, c2) = "") Or (arr(i, c2) <> "" And arr(i, c1) = "") Then
addToRange rngCopy, ws.Range("A" & i)
End If
Next i
arr(1,1)would correspond torw.Cells(c1)(so on Row 6 not Row 1, and column c1 not column 1) You can't use cell coordinates (row/column) if you didn't pick up the array starting at A1.rw.Cells(c1), not from range A1.arrfrom (eg) C3:E6 thenarr(1,1)is the value from C3 andarr(4,3)is the value from E6.