2017-12-28 4 views
0

Ich habe dieses vereinfachte Code, wo infosOptions eine globale Variable mit verschiedenen Typen drin ist:Warum Redim Typ Mismatch Fehler verhindern?

Dim optionsVecteur As Variant 
For j = 0 To UBound(infosOptions) 
    If infosOptions(j, 11) & " Index" = transac.optionName Then 
     optionsVecteur(1) = infosOptions(j, 6) 
     optionsVecteur(2) = infosOptions(j, 5) 
     optionsVecteur(3) = infosOptions(j, 10) 
     Exit For 
    End if 
End j 

ich auf optionsVecteur(1) = infosOptions(j, 6) einen Typenkonflikt Fehler haben, aber wenn Redim optionsVecteur(3) setzen es funktioniert, warum?

+1

Da eine eindimensionale Matrix nicht das gleiche wie eine zweidimensionale Anordnung ist. – braX

+1

Standardmäßig ist 'optionsVecteur' ein eindimensionales Array. Das ist der Grund, warum du 'Redim' brauchst. –

+0

Vielen Dank für Ihre Antworten. Es ist also nicht wirklich ein "Typ" -Diskont, sondern ein Dimensionsfehler! – TmSmth

Antwort

1

Es gibt ein paar Probleme mit Ihrem Code. Wie in den Kommentaren erwähnt, haben Sie eine skalare Variante (wie in einer einzelnen Variablen) und kein Array erstellt. Sie müssen das korrigieren.

Auch Ihre Verwendung von UBound in einem mehrdimensionalen Array kann unter anderen Umständen fehlschlagen. Es empfiehlt sich, die vollständige Definition von UBound zu verwenden, um sicherzustellen, dass Sie tatsächlich das richtige Limit auswählen.

Änderungen wie unten sollten Ihre Probleme sehen aufgelöst:

Dim optionsVecteur() As Variant 'This creates an array of variants - But has not dimensioned the array. 
ReDim optionsVecteur(1 to 3) as Variant 'This dimensions the array as having 3 elements. You could have just put this in the original line above, but doing this allows you to dynamically change the length of the array of required. 

For j = 0 To UBound(infosOptions, 1)'This ensures that VBA considers the upper boundary of the first dimension. 
    If infosOptions(j, 11) & " Index" = transac.optionName Then 
     optionsVecteur(1) = infosOptions(j, 6) 
     optionsVecteur(2) = infosOptions(j, 5) 
     optionsVecteur(3) = infosOptions(j, 10) 
     Exit For 
    End if 
Next j 'You want next j not end j. 
+0

Danke für Ihre Hilfe! "Ende J" war ein Tippfehler. – TmSmth