2014-06-07 18 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

相关问题