变体所需的对象

TmS*_*mth 1 excel vba excel-vba

tableauScores(i - 1, j).Value = wsScores.Cells(2 + i * 3, j + 1).Value在下面的子行中有错误424'对象需要' .

只有这个子,没有别的.我在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
Run Code Online (Sandbox Code Playgroud)

我不明白为什么,我总是使用这样的变体.

Zev*_*itz 5

tableuScores被定义为一个阵列Variant- Variant()-和直到一个给定的数组元素被初始化时,它会具有值Empty.这是不可能阅读的Value财产Empty.

考虑这样做没有Value:

tableauScores(i - 1, j) = wsScores.Cells(2 + i * 3, j + 1).Value
Run Code Online (Sandbox Code Playgroud)