Excel宏搜索文本然后弹出框如果找到则显示单词出现的次数

时间:2013-06-27 16:41:12

标签: excel excel-vba vba

有人可以帮我修改这段代码,这样当它找到单词时它也会显示单词出现的次数吗?谢谢!

Sub findtext()
'
' findrobot Macro
' Will find the word robot
'
' Keyboard Shortcut: Ctrl+t
'
Dim found As Variant

Set found = Sheets("Email").Cells.find("robot", Sheets("Email").Cells(1, 1), xlValues, xlPart)

If (Not found Is Nothing) Then


    'found
    MsgBox "Words Found =" & found, vbOKOnly, "Found"


Else

    'not found
MsgBox "Sorry the text was not found please try again. Macro stopping"

 End If

End Sub

1 个答案:

答案 0 :(得分:0)

只是为了好玩(并没有真正的压力测试,但似乎工作),这是一个非循环版本:

Sub test()
Dim Number  As Long
Dim str     As String

str = "robot"

Number = Evaluate("=SUMPRODUCT(--(NOT(ISERROR(FIND(""" & str & """,A1:AZ6,1)))))")

MsgBox str & " was found " & Number & " times!"

End Sub

请务必在评估线(目前为A1:AZ6)中调整范围以适合您的情况。此外,我很确定如果您要查找的单词在单元格中出现多次,则不会返回正确的结果。我必须参加一个会议,但如果你认为会是这样,我可以看看我是否可以稍后解决这个问题......