TmSmth
TmSmth

Reputation: 452

Object Required on a Variant

I have the error 424 'object required' on the line tableauScores(i - 1, j).Value = wsScores.Cells(2 + i * 3, j + 1).Value in the following sub.

There is only this sub, nothing else. I am in Base 0.

    Dim wsScores As Worksheet
    Dim i As Integer, j As Integer
    Dim tableauScores As Variant

    Set wsScores = ThisWorkbook.Worksheets("Scores")        
    j = 0

    ReDim tableauScores(1, 2)

    If wsScores.Cells(2, 1).Value = "Thomas" Then        
        For i = 1 To 2
            For j = 1 To 3
                tableauScores(i - 1, j).Value = wsScores.Cells(2 + i * 3, j + 1).Value
            Next j
        Next i            
    End If

End Sub

I don't get why, I always use variant like this.

Upvotes: 1

Views: 1747

Answers (1)

Zev Spitz
Zev Spitz

Reputation: 15317

tableuScores is defined as an array of VariantVariant() — and until a given array element is initialized, it will have a value of Empty. It's impossible to read the Value property of Empty.

Consider doing this without Value:

tableauScores(i - 1, j) = wsScores.Cells(2 + i * 3, j + 1).Value

Upvotes: 5

Related Questions