2017-12-28 5 views
0

私はinfosOptionsはそれで別のタイプのグローバル変数で、この単純化されたコードを持っている:それはなぜ、働くRedim optionsVecteur(3)を置く場合、私はoptionsVecteur(1) = infosOptions(j, 6)の型の不一致エラーを持っていますが、redimがタイプミスマッチエラーを防止するのはなぜですか?

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 

を?

+1

1次元配列は、2次元配列と同じものではありませんので。 – braX

+1

デフォルトでは、 'optionsVecteur'は1次元配列です。これが、「Redim」に必要な理由です。 –

+0

お返事ありがとうございます。だから、実際には「タイプ」の不一致ではなく、寸法誤差です! – TmSmth

答えて

1

コードにはいくつか問題があります。コメントで指摘されているように、配列ではなくスカラVariant(単一の変数のように)を作成しました。これを修正する必要があります。

また、多次元配列でのUBoundの使用は、他の状況で失敗する可能性があります。 UBoundの完全な定義を使用して、実際に正しい制限を確実に選択することをお勧めします。以下のような

変更があなたの問題が解決されるはずです。

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

ご協力いただきありがとうございます! "End J"はタイプミスでした。 – TmSmth

関連する問題