2017-10-13 1 views
0

Warum bekomme ich "Subskript out of range" in meiner Zeile T(k) = Cells(k + 1, 4).Value - z?VBA _Error 9 Index außerhalb des Bereichs

Public Sub find() 
Dim i, j, k, h As Integer 
Dim T() As Double 
Dim z As Double 
Range("E1").Activate 
i = ActiveCell.Row 
j = ActiveCell.Column 
While Not IsEmpty(Cells(i, j - 2).Value) 
    z = Cells(i, j - 2).Value 
    k = 0 
While Not IsEmpty(Cells(k + 1, 4).Value) 
    T(k) = Cells(k + 1, 4).Value - z 
    k = k + 1 
Wend 
    For h = 0 To k 
    If T(h) = Application.WorksheetFunction.Min(Abs(T(k))) Then 
    Cells(i, j).Value = Cells(h + 1, 4).Value 
    End If 
    Next 
i = i + 1 
Wend 
End Sub 
+0

Mögliche Duplikat [Bestücken von VBA dynamische Arrays] (https://stackoverflow.com/questions/8850984/populating-vba-dynamic-arrays) –

+0

Diese Frage richtet sich Ihr Problem: https: // stackoverflow.com/questions/8850984/populating-vba-dynamic-arrays/8851086#8851086 Achten Sie darauf, mehr als nur die erste Antwort zu lesen! –

Antwort

0

An dem Punkt, wo Sie T(k) = ... sagen, T Ihr Array wurde noch nicht zugeordnet. So etwas wie T(0) gibt es noch nicht. Daher der Fehler "Index außerhalb des Bereichs".

Vor dem Indexieren in T, geben Sie T eine Größe mit ReDim. Zum Beispiel:

Dim T() As Double 
ReDim T(0 to 123) ' or whatever size you need 
Verwandte Themen