2016-05-13 89 views
2

不确定如何短语我的问题 - 所以我很抱歉,如果它似乎有点模糊。我似乎还有其他的方法,比如检查多个组合框等,但没有特别涉及到我正在寻找的东西,不确定我是否试图做一些不可能的事情。Excel VBA - 检查combolist值并添加(如果尚未添加)

我想要做的是检查我的组合框,看看一个值是否已经存在,如果没有,并添加它。

工作簿打开并调用UserForm1.show - 这会触发Userform初始化设置一些文本框默认值(空和设置像autosize等)这很好,在这个过程中我称之为个人函数DrpDwn_init - 想法是DrpDwn_init在组合框中选中并设置值。我可以调用该函数,甚至可以添加值,但我无法弄清楚如何检查值是否已经存在。

我目前所面对的是以下几点:

Public Function DrpDwn_Init() 

Dim Templates() As String 
Templates = Split("Stuff 1*Stuff 2", "*") 

For i = 0 To UBound(Templates) 
If Templates(i) <> UserForm1.DrpDwn_Templates.List(i) Then 
    MsgBox "Does Not match" 
Else 
    MsgBox "Does Match" 
End If 
Next i 

End Function 

也使用

For i = 0 To UBound(Templates) 
If CStr(Templates(i)) <> UserForm1.DrpDwn_Templates.List(i) Then 

For i = 0 To UBound(Templates) 
If Templates(i) <> CStr(UserForm1.DrpDwn_Templates.List(i)) Then 

For i = 0 To UBound(Templates) 
If CStr(Templates(i)) <> CStr(UserForm1.DrpDwn_Templates.List(i)) Then 

以及使用“UserForm1.DrpDwn_Templates.ListIndex(I)

我尝试已经尝试过很多方法,有时我得到的唯一错误是,突然Userform1.show变得不可接受!?即使我从来没有碰过这部分代码,我真的无法弄清楚如何循环访问数组,检查每个数组索引项以查看它是否存在于组合框中,并根据它是否是否执行任务。

真的希望有人轮在这里可以帮助我搞清楚了这一点 (顺便说一句,我已经检查了我的所有代码引用函数等是正确的,如果我在这里做错别字我道歉。)

回答

2

你AREN” t每次循环下拉框中的所有项目。考虑有以下几种下面的示例

表1

Apples 
Pears 

表2

Oranges 
Apples 

你需要做的检查:

Apples = Oranges (FALSE) 
Apples = Apples (TRUE) 
Pears = Oranges (FALSE) 
Pears = Apples (FALSE) 

那么简单循环这里我有点儿像

For i = 1 to 2 
    For j = 1 to 2 
     if list1(i) = list2(j) then 
      inList = TRUE 
     End if 
    Next j 
    If inList = TRUE then 
     MsgBox "Found in the list" 
    End if 
Next i 

修订下面的代码应与单个用户窗体,并在该用户窗体(分别为UserForm1和ComboBox1)的组合框

Sub test() 

Dim i As Integer, j As Integer 
Dim inList As Boolean 
Dim vListItems As Variant, vTestItems As Variant, vItem As Variant 

vListItems = Array("Apple", "Orange") 
vTestItems = Array("Pear", "Apple") 

'populate ComboBox1 
For Each vItem In vListItems 
    UserForm1.ComboBox1.AddItem vItem 
Next vItem 

For i = 0 To UBound(vTestItems) 
    inList = False 
    'check if item is in your dropdown list already 
    For j = 0 To UserForm1.ComboBox1.ListCount - 1 
     If vTestItems(i) = UserForm1.ComboBox1.List(j) Then 
      inList = True 
     End If 
    Next j 
    'insert into list if not found 
    If inList = False Then 
     UserForm1.ComboBox1.AddItem vTestItems(i) 
    End If 
Next i 

UserForm1.Show 

End Sub 
+0

我没有意识到我需要循环浏览下拉菜单中的所有选项 - 我认为这在事后看来是有道理的 - 代码看起来很合理 - 我可以跟随它发生的事情。 *按下输入换行符 - 卫生署* 我已经尝试了代码,但我得到: “运行时错误‘13’: 类型不匹配” 强调了: 对于j = 0到UserForm1.DrpDwn_Templates。 ListCount –

+0

这可能是因为.ListCount返回一个长整数,而i/j是一个整数。您可能想要将i/j声明为Long /查看是否可以找到更好的方法来获取列表框中的项目总数,或者使用'cInt()'将表达式转换为整数 – CallumDA

+0

我刚刚尝试过做同样的事情,但是: 'Dim m as int' 'm = UserForm1.DrpDwn_Templates.ListCount' 这也给类型不匹配? 加入'MsgBox UserForm1.DrpDwn_Templates.ListCount' 这显示在一个msgbox中的列表计数 - 但然后由于某种原因抛出类型不匹配后,我确定MsgBox - 这真是莫名其妙,这似乎应该是一个相当简单努力 –

0

我尝试这样做,我就开始工作工作。犯规满足您的需求,变型,但让你更接近计算出来

Private Sub UserForm_Activate() 
    ComboBox1.Clear 
    StuffArray(0) = "Stuff" 
    StuffArray(1) = "Another thing" 
    For i = LBound(StuffArray, 1) To UBound(StuffArray, 1) 
     With ComboBox1 
      .AddItem StuffArray(i) 
     End With 
    Next i 

    Dim Thing As String 
    Thing = ThisWorkbook.Sheets("Sheet3").Range("u9").Value 
    For i = LBound(StuffArray, 1) To UBound(StuffArray, 1) 
     If Thing <> StuffArray(i) Then 
      With ComboBox1 
       .AddItem Thing 
      End With 
     ElseIf Thing = StuffArray(i) Then 
     End If 
     Next 
End Sub 

编辑 - 原来,这增加了原来的两倍,但多数民众赞成一个简单的办法:P

enter image description here

+0

原谅我 - 我曾使用 'Dim Templates()as string' 'Templates = Split(“Stuff * andStuff”,“*”)' 因为我的印象是这个DID不使用变体数据类型 - 纠正我,如果我错了,但变种使用更多的内存比绝对必要的权利?因此我使用拆分方法。 –

+0

正确。在我发布之前,我也没有读过CallumDA33的答案(因为他在我打字的时候发帖)。我没有尝试过,但可能比我更适合INF。我觉得我花了几分钟的时间Im无论如何张贴它LOL –