0

I tried to find which rows have cell value in Column A matches one of the cell values in another column in the same worksheet.

I got error

Subscript out of range

and don't know why this occurs.

Here is the code:

Sub test()  
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim FoundCell As Range
    Set wb = ActiveWorkbook
    Set ws = ActiveSheet
    Dim colA As Variant, rowArr() As Variant, ub As Long, Counter As Long, j As Long, i As Long, Num As Variant
    colA = ws.UsedRange.Columns("A").Value2
    ub = UBound(colA)

    j = 1
    For Counter = 1 To ub
        Num = ws.Cells(Counter, 1).Value
        If Not IsError(Application.Match(Num, ws.Columns(6), 0)) Then
            rowArr(j) = Counter
            j = j + 1
        End If
    Next Counter

    ReDim Preserve rowArr(1 To j - 1)


    For i = 1 To j
        With ws.Range("A" & rowArr(i) & ":B" & rowArr(i))
            .Font.Size = 12
            .Font.Shadow = True
        End With
    Next
End Sub

The error occurs in the line rowArr(j) = Counter. Anyone knows what's the problem?

Thanks in advance!

7
  • What is the value of j when it fails? Basically the error is saying the index position doesn't exist in your array. Commented Sep 6, 2018 at 8:25
  • j is 1, Counter is 2 when it fails. Actually cell A2 matches one of the cell values in column F. Commented Sep 6, 2018 at 8:27
  • 1
    You are dimensioning rowArr only after trying to use it... Commented Sep 6, 2018 at 8:27
  • 3
    You have to redim rowArr before trying to access it as an array. Commented Sep 6, 2018 at 8:27
  • 1
    @open0121 the dynamic size means that initially the array does not have any size. Commented Sep 6, 2018 at 8:38

1 Answer 1

2

You need to redim your array before you populate values:

j = 1
For Counter = 1 To ub
    Num = ws.Cells(Counter, 1).Value
    If Not IsError(Application.Match(Num, ws.Columns(6), 0)) Then
        ReDim Preserve rowArr(1 To j)
        rowArr(j) = Counter
        j = j + 1
    End If
Next Counter
Sign up to request clarification or add additional context in comments.

7 Comments

Now the same error goes to line 'With ws.Range("A" & rowArr(i) & ":B" & rowArr(i))'. What would be the problem this time?
j exceeds the upper bound of array rowArr. You can change this by changing how you increment j in your initial loop, or you can loop i from 1 to j-1
I also find another problem that the Application.Match only find the match value for the first item in the searching range 'ws.Columns(6)', while the other matches are missing... Do you have any idea how to solve the problem?
thanks! I kind of understand your point, but don't exactly know how to correct the code (I'm new to Macros)
I found the solution! Thanks to your tips Olly!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.