检查动态添加的ComboBox中的用户输入数据

时间:2014-06-07 14:00:09

标签: excel vba excel-vba user-interface word-vba

我正在使用用户表单来显示文档中的首字母缩略词和首字母缩略词的定义。因为我事先不知道有多少我将使用下面的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

1 个答案:

答案 0 :(得分: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