向combobox中添加字符串,如果发现已经存在就不再添加了,不知道怎么做循环,请教

解决方案 »

  1.   

    Dim s As String
    Dim i As Long
    s = "要加入的字符串"
    For i = Combo1.ListCount - 1 To 0 Step -1
        If s = Combo1.List(i) Then Exit For
    Next
    If i = -1 Then
        Combo1.AddItem s
    End If
      

  2.   

    补充下
    if i = 0 then
      

  3.   

    Option ExplicitPrivate Sub Command1_Click()
            Dim i As Long
            Dim S As String
            
            S = "12345" '需要增加的项
            
            For i = 0 To Combo1.ListCount - 1
                If Combo1.List(i) <> S Then
                   '继续循环
                Else
                   Exit Sub '不添加
                End If
            Next
            
            Combo1.AddItem S '添加项S
            
    End Sub