Excel VBA通配符search
我目前有一列和许多行的excel文件,列中有名字和姓氏,可能还有一个中间名(例如:John Abe Smith)。 我正在写一个有1个文本框和1个button的macros。 让我们说在Excel表中,我有几个名字:
John Abe Smith Cindy Troll Bee Randy Row Joe Jumbo Katie Kool Kat
我想写一个macros,当我在文本框中键入一些东西,然后单击button,它将在此列中查找该名称,并有一个msgbox只是说“发现”,然后EXIT SUB
但在search名称时,我想使用通配符“*”,但我不知道如何。 我目前有这样的代码,但通配符不起作用:
Private Sub search_Click() For firstloop = 3 To 10 If Range("G" & firstloop).Text = name.Text & "*" Then MsgBox "Found!" Exit Sub Else MsgBox "NOT FOUND" End If Next End Sub
比方说,我在文本框中input“巨魔”,我点击button,我想循环通过列find任何与“巨魔”在其中。 我怎么能这样做?
你可以使用Like
运算符( 区分大小写 ):
Private Sub search_Click() For firstloop = 3 To 10 If Range("G" & firstloop).Text Like name.Text & "*" Then MsgBox "Found!" Exit Sub Else MsgBox "NOT FOUND" End If Next End Sub
对于不区分大小写的search使用:
If UCase(Range("G" & firstloop).Text) Like UCase(name.Text) & "*" Then
另外,如果您想确定单元格是否包含文本(不仅以文本开头 ),您可以使用( 区分大小写 ):
If InStr(1, Range("G" & firstloop).Text, name.Text) > 0 Then
或( 不区分大小写 )
If InStr(1, Range("G" & firstloop).Text, name.Text, vbTextCompare) > 0 Then
UPD:
如果点只显示msgbox
,那么我build议使用Application.Match
:
Private Sub search_Click() If Not IsError(Application.Match("abc" & "*", Range("G3:G10"), 0)) Then MsgBox "Found!" Else MsgBox "NOT FOUND" End If End Sub
您也可以避免循环,并使用Range.Find方法。 如果你想区分大小写,你可以通过选项。 (默认情况下不区分大小写。)
Set rngFound= Range("G" & firstloop).Find(name.Text & "*") If rngFound Is Nothing Then MsgBox "Not Found!" Else MsgBox "FOUND" End If
- 错误date和时间文本到列macrosExcel VBA
- Excelmacros从Excel工作表中复制表格,并将其粘贴到PowerPoint幻灯片的灵活性,以dicede哪colomuns和行
- 如何通过VBA获取当前Excel实例的进程ID,而不使用标题?
- countif:统计一列中与其他单元格的值相匹配的所有值的出现次数?
- DictReader Excel文件
- VBA检查一个十进制值是否只有2个小数点。 使用Int()时出错
- 当使用c#从Excel中读取大于12位的数字时,数字被转换为零
- 无法从公式调用的函数中隐藏行Excel 2003
- VBA代码引用从单元格值列表中取path\文件\范围的封闭工作表范围