2013-08-21 125 views
0

我在Excel VBA中遇到了ComboBox問題。任何幫助,高度讚賞。 這就是我要做的:從第一個ComboBox動態填充第二個Combobox變量

  • 在用戶窗體,用戶名爲「ComboBox_groesse」(普通的組合框固定行來源的輸入是數字)第一個組合框選擇一個輸入
  • 當DropButton單擊名爲「ComboBox_config_1」的第二個ComboBox組合框,該組合框將填充來自NamedRange「KonfiRange_1_1」的值,該值等於所選ComboBox_groesse.value的值
  • NamedRange位於具有2列的表中。第一列是帶數字的NamedRange,第二列帶有文本。
  • 我想對應的文本在第二組合框一起出現與相等的值,以ComboBox_groesse.value

我已經寫了下面的代碼,但我得到一個空白ComboBox_config_1。任何人都可以幫忙嗎?提前致謝!

Private Sub ComboBox_config_1_DropButtonClick() 

    Dim teil As Range 

    For Each teil In Tabelle1.Range("KonfiRange_1_1") 

    If teil.Value = ComboBox_groesse.Value Then 

     With Me.ComboBox_config_1 

     .AddItem teil.Value 

     End With 

    End If 
    Next teil 

回答

0

我會把這個操作在ComboBox_groesse_Change事件,像這樣:

Private Sub ComboBox_groesse_Change() 

    Dim rngFound As Range 
    Dim strFirst As String 
    Dim strList As String 

    Me.ComboBox_config_1.Clear 
    If Me.ComboBox_groesse.ListIndex = -1 Then Exit Sub 'Nothing selected 

    With Range("KonfiRange_1_1") 
     Set rngFound = .Find(Me.ComboBox_groesse.Text, .Cells(.Cells.Count), xlValues, xlWhole) 
     If Not rngFound Is Nothing Then 
      strFirst = rngFound.Address 
      Do 
       strList = strList & "|" & rngFound.Offset(, 1).Text 
       Set rngFound = .Find(Me.ComboBox_groesse.Text, rngFound, xlValues, xlWhole) 
      Loop While rngFound.Address <> strFirst 
     End If 
    End With 

    If Len(strList) > 0 Then Me.ComboBox_config_1.List = Split(Mid(strList, 2), "|") 

    Set rngFound = Nothing 

End Sub 
+0

它的偉大工程。感謝tigeravatar! –

相關問題