1

有人可以帮我更改此代码,以便在找到单词时还会显示该单词出现的次数?谢谢!

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
4

1 回答 1

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

请务必调整 Evaluate 行(当前为 A1:AZ6)中的范围以适合您的情况。另外,如果您要查找的单词在单元格中多次出现,我很确定这不会返回正确的结果。我得跑去开会,但如果你认为会是这样,我可以看看我是否可以稍后解决...

于 2013-06-27T17:11:32.653 回答