如何在使用 Excel VBA 搜索后清除搜索条件和单词

How to clear search conditions and word after a search using Excel VBA

场景

我有一个用户窗体,除了其他自动工作之外,它上面的一个按钮还可以使用 MatchCase = True 执行搜索功能。每当此用户表单处于活动状态并且我尝试在其他打开的工作簿中进行一些搜索时,总是会勾选匹配大小写。每次我需要手动取消选中 excel 中的 Match case 时,此用户表单处于活动状态。请看图片

当用户表单未显示时

显示用户表单时

问题

有什么方法可以 运行 我的宏设置 MatchCase = True 但同时打开其他工作簿并按 Ctrl+F 默认情况下未选中 Matchcase?换句话说如何在搜索宏执行后删除搜索关键字和搜索条件?我使用以下搜索代码将搜索词作为变量 worD

Selection.Find(What:=worD, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False).Activate

完成后执行虚拟搜索

Sub clearFind(ws As Worksheet)
    With ws.Cells
        .Find What:="", After:=ActiveCell, LookIn:=xlFormulas, _
              LookAt:=xlPart, SearchOrder:=xlByRows, _
              SearchDirection:=xlNext, MatchCase:=False, _
              SearchFormat:=False
    End With
End Sub

理论上也许您可以使用 SendKeys,但根据我的经验,它并不可靠。

 ' here set a variable to search. After that:
      Application.SendKeys ("^f")
      Application.SendKeys ("{DELETE}")
 ' and finally paste variable.value via SendKeys

根据堆栈溢出中的一些成员,一种简单的方法是虚拟搜索,它清除匹配案例和搜索历史记录。

Sub dummySearch()
    dummy = Cells.Find(What:="", LookIn:=xlValues, LookAt:=xlPart)
End Sub