vba 如何检查组合框列表中是否已存在数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19308611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check whether the data already exists in combobox list?
提问by blackmaler
How can I check if the data from cmbTypeYacht.text
already exists in cmbTypeYacht.list
?
如何检查 中的数据是否cmbTypeYacht.text
已存在cmbTypeYacht.list
?
Here's what I've got:
这是我所拥有的:
Dim TypeYacht As String 'Type of yacht input
TypeYacht = cmbTypeYacht.Text
If TypeYacht = ("cmbTypeYacht list") Then
MsgBox "Type of Yacht is already on the list", vbExclamation, "Yacht Chantering"
Else
cmbTypeYacht.AddItem cmbTypeYacht.Text
With cmbTypeYacht
.Text = ""
.SetFocus
End With
End If
sorry about the tag im not quite sure which is it but im using Microsoft Visual Basic app.
抱歉,我不太确定是哪个标签,但我正在使用 Microsoft Visual Basic 应用程序。
回答by Karl Anderson
The ComboBox
class has a FindStringExact()
method that will do the trick for you, like this:
该ComboBox
班有一个FindStringExact()
会做的伎俩对你来说,这样的方法:
Dim resultIndex As Integer = -1
resultIndex = cmbTypeYacht.FindStringExact(cmbTypeYacht.Text)
If resultIndex > -1 Then
' Found text, do something here
MessageBox.Show("Found It")
Else
' Did not find text, do something here
MessageBox.Show("Did Not Find It")
End If
You can also just loop through the list as well, like this:
您也可以循环遍历列表,如下所示:
Dim i As Integer = 0
For i = 0 To cmbTypeYacht.Items.Count - 1
If cmbTypeYacht.Items.Contains(cmbTypeYacht.Text) Then
MessageBox.Show("Found It")
Exit For
End If
Next
回答by SM177Y
I'm working in Excel 2013 and there is no FindStringExact or .Items.Contains so, neither of those are valid. There is also no need to iterate the list. It is very simple actually. Given a userform "MyUserForm" and a combobox "MyComboBox",
我在 Excel 2013 中工作,并且没有 FindStringExact 或 .Items.Contains 所以,这两个都不是有效的。也不需要迭代列表。其实很简单。给定一个用户表单“MyUserForm”和一个组合框“MyComboBox”,
If MyUserForm.MyComboBox.ListIndex >= 0 Then
MsgBox "Item is in the list"
Else
MsgBox "Item is NOT in the list"
End If
Explanation: If selected item is not in the list, .ListIndex returns -1.
说明:如果所选项目不在列表中,.ListIndex 返回 -1。
回答by Chuan Ng
The combobox in vba has a property called MatchFound. It will return true if the value you inputted in the combobox (ComboBox.Value
) existed before.
vba 中的组合框有一个名为 MatchFound 的属性。如果您在组合框 ( ComboBox.Value
) 中输入的值之前存在,它将返回 true 。
Put below code in the update event of the combobox for trial
将下面的代码放在组合框的更新事件中进行试用
Private Sub ComboBox_AfterUpdate()
If ComboBox.MatchFound = True then
Msgbox "Value exist"
End If
End Sub
Check it out: https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/combobox-matchfound-property-outlook-forms-script
检查一下:https: //msdn.microsoft.com/en-us/vba/outlook-vba/articles/combobox-matchfound-property-outlook-forms-script
回答by Jeremy Stroud
You do not need to iterate through combobox.items. Items.Contains will already iterate through the list for you.
您不需要遍历 combobox.items。Items.Contains 将已经为您遍历列表。
Simply use:
只需使用:
If cmbTypeYacht.Items.Contains(cmbTypeYacht.Text) Then
MessageBox.Show("Found It")
Exit For
End If
回答by Andrew Day
Searching: VBA check whether the data already exists in combobox list?
but vba doesnt have the properties above.
搜索:VBA 检查组合框列表中是否已存在数据?
但 vba 没有上述属性。
Sub TestString()
Dim myString As String
Dim i As Long
Dim strFound As Boolean
'Just for test purposes
myString = "Apple"
strFound = False
With Me.ComboBox1
'Loop through combobox
For i = 0 To .ListCount - 1
If .List(i) = myString Then
strFound = True
Exit For
End If
Next i
'Check if we should add item
If Not strFound Then .AddItem (myString)
End With
End Sub
This was found after a lot of searching at http://www.ozgrid.com/forum/showthread.php?t=187763and actually works
这是在http://www.ozgrid.com/forum/showthread.php?t=187763 上大量搜索后发现的, 并且确实有效