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

问题描述 投票:0回答:3

场景

我有一个用户窗体,除其他自动化工作外,该窗体上的按钮还具有MatchCase = True的搜索功能。每当此用户窗体处于活动状态并且我尝试在其他打开的工作簿中进行搜索时,总是会选中“区分大小写”。每当此用户窗体处于活动状态时,我每次都需要在excel中手动取消选中“匹配”大小写。请看图片

未显示用户窗体时

enter image description here

显示用户表单时

enter image description here

问题

有什么方法可以使用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
excel vba excel-vba userform
3个回答
3
投票

完成后执行虚拟搜索

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

0
投票

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

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

0
投票

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

Sub dummySearch()
    dummy = Cells.Find(What:="", LookIn:=xlValues, LookAt:=xlPart)
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.