2017-09-01 17 views
3

以下のコードは、私がオンラインで見つけたいくつかの例からtogheterをパッチしました。私はVBAの専門家ではありません。VBAの最初の配列項目は常に空です

clistアレイの最初の項目(とドロップダウンの最初の項目)は常に空ですが、redimと何か関係があると仮定していますが、わかりませんでした。

何が問題になりますか?

Private Sub ComboBox1_Change() 
    ReDim clist(0) 
    'If any value is input 
    If ComboBox1.Value <> "" Then 
     Dim kword As Variant 
     Dim product As Variant 
     'For each product description in our sheet table 
     For Each product In [Produtos[Descrição]].Rows 
      'Keyword search 
      For Each kword In Split(ComboBox1.Value, " ") 
       If InStr(Replace(product.Value, "", " "), kword) And kword <> "" Then 
        'Issue most likely here 
        ReDim Preserve clist(UBound(clist) + 1) As Variant 
        clist(UBound(clist)) = product.Value 
        Exit For 
       End If 
      Next kword 
     Next product 
     ComboBox1.list = clist 
     'If found something 
     If UBound(clist) > 0 Then 
      ComboBox1.DropDown 
     End If 
    'If no Input just show all products, here it doesn't show a blank item 
    Else 
     ComboBox1.list = [Produtos[Descrição]].Value2 
    End If 
End Sub 

答えて

2

、としてそれを試してみてください。

ReDim Preserve clist(UBound(clist) + 1) As Variant 'Increase array size by 1 
clist(UBound(clist)) = product.Value 'Set a value to the higher index 

あなたはこのような何かがあなたの問題を解決する必要がありindex 0

に値を設定することはありません。この方法は:

if clist(Ubound(clist)) <> empty then 
    ReDim Preserve clist(UBound(clist) + 1) As Variant 
end if 
clist(UBound(clist)) = product.Value 
4

あなたは、配列のサイズを増やすだけにして、それの最後のインデックスに値を設定しているので、それがある

ReDim clist(0) 
    For Each product In [Produtos[Descrição]].Rows 
     'Keyword search 
     For Each kword In Split(ComboBox1.Value, " ") 
      If InStr(Replace(product.Value, "", " "), kword) And kword <> "" Then 
       'Issue most likely here 
       clist(UBound(clist)) = product.Value 
       ReDim Preserve clist(UBound(clist) + 1) 
       Exit For 
      End If 
     Next kword 
    Next product 
    ReDim Preserve clist(UBound(clist) - 1) 
+0

CLISTが大型化されることはありません場合、これはエラーになります – Mojimi

関連する問題