2014-06-07 22 views
0

我正在使用用戶窗體來顯示在文檔中找到的首字母縮略詞以及首字母縮寫詞的定義。因爲我不會提前知道有多少人會使用下面的for循環動態地創建所有標籤,複選框和組合框。檢查動態添加的組合框中的用戶輸入數據

我現在陷入困境,我想讓用戶能夠鍵入comboBox一個新的定義是例如一個不存在於我的Excel數據庫或他們想要使用一個不同的定義那是在那裏(我知道這是不好的做法,但不幸的是人們不堅持標準清單)。現在,所有的工作正常,但它的設置正確,但我的問題是,我想檢查用戶是否輸入了新的或不新的。

所以我的問題是,是否有內置的函數或變量,這樣做?還是有一個簡單的方法來做到這一點? (我已經嘗試和測試代碼字符串添加到我的數據庫,所以這不是一個問題,只是如果它之前是不存在不通過整個數據庫從頭再次運行檢查)

For i = 1 To n 

     checkBoxi = "CheckBox" & i 
     labeli = "Label" & i 
     comboBoxi = "ComboBox" & i 

     'add checkbox, label and combobox 
     .MultiPage1.Pages("Page1").Controls.Add "Forms.CheckBox.1", checkBoxi 
     .MultiPage1.Pages("Page1").Controls.Add "Forms.Label.1", labeli 
     .MultiPage1.Pages("Page1").Controls.Add "Forms.ComboBox.1", comboBoxi 

     'position check box 
     .MultiPage1.Pages("Page1").Controls(checkBoxi).Left = LeftSpacing 
     .MultiPage1.Pages("Page1").Controls(checkBoxi).Top = TopSpacing + rowHeight * i 

     'position labels 
     .MultiPage1.Pages("Page1").Controls(labeli).Left = LeftSpacing + 15 
     .MultiPage1.Pages("Page1").Controls(labeli).Top = TopSpacing + 2 + rowHeight * i 
     .MultiPage1.Pages("Page1").Controls(labeli).Caption = acronyms(i - 1) 
     .MultiPage1.Pages("Page1").Controls(labeli).Width = 70 

     'position comboBox 
     .MultiPage1.Pages("Page1").Controls(comboBoxi).Left = LeftSpacing + 100 
     .MultiPage1.Pages("Page1").Controls(comboBoxi).Top = TopSpacing + rowHeight * i 
     .MultiPage1.Pages("Page1").Controls(comboBoxi).Width = 300 

'find definitions for comboBox 
    ' Find the definition from the Excel document 
    With objWbk.Sheets("Sheet1") 
     ' Find the range of the cells with data in Excel doc 
     Set rngSearch = .Range(.Range("A1"), .Range("A" & .rows.Count).End(-4162)) 

     ' Search in the found range for the 
     Set rngFound = rngSearch.Find(What:=acronyms(i - 1), After:=.Range("A1"), LookAt:=1) 

     ' if nothing is found count the number of acronyms without definitions 
     If rngFound Is Nothing Then 


      ' Set the cell variable in the new table as blank 
      ReDim targetCellValue(0) As String 
      targetCellValue(0) = "" 

     ' If a definition is found enter it into the cell variable 
     Else 

      targetCellValue(0) = .Cells(rngFound.Row, 2).Value 
      'MsgBox (targetCellValue(0) & " " & 0) 
      firstAddress = rngFound.Address 


      Do Until rngFound Is Nothing 
       Set rngFound = rngSearch.FindNext(After:=rngFound) 

       If rngFound.Address = firstAddress Then 
        Exit Do 
       ElseIf rngFound.Address <> firstAddress Then 
        j = j + 1 
        ReDim Preserve targetCellValue(0 To j) As String 
        targetCellValue(j) = .Cells(rngFound.Row, 2).Value 
        'MsgBox (targetCellValue(j) & " " & j) 

       End If 
      Loop 

      End If 
     End With 

     Dim k As Integer 
     For k = 0 To j 
      .MultiPage1.Pages("Page1").Controls(comboBoxi).AddItem targetCellValue(k) 
     Next k 
     j = 0 

    Next i 
+0

我的這篇文章可能會有所幫助:http://yoursumbuddy.com/prompt-to-add-new-items-to-combobox-or-data-validation/ –

+0

我將這些組合框放在彈出式用戶窗體中,而不是在Excel本身中。然而,我確實在研究這個概念,並且有一個屬性'Controls(comboBoxi).MatchRequired = True',這與我的想法類似,但是這隻會阻止我輸入一個不在下拉選項中的值。是否可以設置打開錯誤,甚至爲此將它添加到列表並存儲它? –

+0

在那篇文章中'Comobobox'也是一個(很小)的用戶表單。它假定您將列表保存在Excel表格中。您可以嘗試下載示例工作簿並與其混淆。對不起,我現在不能花更多的時間。 –

回答

1

我找到了一個辦法。由用戶鍵入的值不會自動包含在組合框列表中,因此您可以根據列表檢查它是否存在。

代碼:

For intComboItem = 0 To .MultiPage1.Pages("Page1").Controls(comboBoxi).ListCount - 1 

    If .MultiPage1.Pages("Page1").Controls(comboBoxi).Value = .MultiPage1.Pages("Page1").Controls(comboBoxi).List(intComboItem) Then 
     newDef = False 
     Exit For 
    Else 
     newDef = True 
    End If 

Next 

If newDef Then 
    MsgBox ("new def: " & .MultiPage1.Pages("Page1").Controls(comboBoxi).Value) 
End If 
+0

只有稍微好一點:'newDef = True''For intComboItem = 0 To ..''If .. = .. Then..'newDef = False''Exit For'' End If'' Next'' – dcromley

相關問題