重置Excel“查找和替换”对话框参数

4
我该如何以编程方式将Excel中的“查找和替换”对话框参数重置为默认值(“查找内容”,“替换为”,“在范围内”,“搜索”,“查找位置”,“大小写匹配”、“整个单元格匹配”)?
我正在使用Application.FindFormat.Clear和Application.ReplaceFormat.Clear来重置查找和替换单元格格式。
有趣的是,在使用expression.Replace(FindWhat,ReplaceWhat,After,MatchCase,WholeWords)之后,“FindWhat”字符串显示在“查找和替换”对话框中,但不显示“ReplaceWhat”参数。
5个回答

7
您可以使用此宏重置查找和替换。不幸的是,您必须同时调用它们,因为每个都有一个或两个独特的参数,因此如果您想要重置所有内容,您就会陷入困境。没有“重置”选项,所以我发现唯一的方法是使用默认参数执行虚假的查找和替换。
Sub ResetFind()
    Dim r As Range

    On Error Resume Next  'just in case there is no active cell
    Set r = ActiveCell
    On Error Goto 0

    Cells.Find what:="", _
               After:=ActiveCell, _
               LookIn:=xlFormulas, _
               LookAt:=xlPart, _
               SearchOrder:=xlByRows, _
               SearchDirection:=xlNext, _
               MatchCase:=False, _
               SearchFormat:=False
    Cells.Replace what:="", Replacement:="", ReplaceFormat:=False

    If Not r Is Nothing Then r.Select
    Set r = Nothing
End Sub

1
您可以使用以下命令打开填充有字段的“替换”对话框: Application.Dialogs(xlDialogFormulaReplace).Show -在此处输入参数- 参数列表如下:
查找文本、替换文本、查看方式、由谁查找、活动单元格、匹配大小写、匹配字节

到目前为止,我发现唯一点击按钮的方法是使用SendKey。


经过大量的研究和测试,我现在知道你想做什么,但是我认为它无法完成(没有使用SendKey)。似乎Excel中存在一个错误,无论你尝试将替换值设置为什么,它都不会从VBA重置。

我在MSDN上找到了这个“更快”的方法,所以你可以试试。

比Replace更快


非常感谢。我正在寻找在使用VBA的查找和替换后进行清理的方法。希望有一种解决方案,不需要弹出对话框。之前不知道Show方法后面的参数。至少可以避免使用SendKeys(蛮力)。来自南非约翰内斯堡。 - spagh

0
Dave Parillo的解决方案非常好,但它不会重置Excel的查找和替换对话框的格式选项。以下方法更彻底,并重置这些选项。
Sub ResetFindAndReplace()
    Dim oldActive As Range, oldSelection As Range
    On Error Resume Next  ' just in case there is no active cell
    Set oldActive = ActiveCell
    Set oldSelection = Selection
    On Error GoTo 0   
    Cells.Find what:="", _
               After:=ActiveCell, _
               LookIn:=xlFormulas, _
               LookAt:=xlPart, _
               SearchOrder:=xlByRows, _
               SearchDirection:=xlNext, _
               MatchCase:=False, _
               SearchFormat:=False
    Cells.Replace what:="", Replacement:="", ReplaceFormat:=False
    Application.FindFormat.Clear
    ' return selection cell
    If Not oldSelection Is Nothing Then oldSelection.Select 
    ' return active cell
    If Not oldActive Is Nothing Then oldActive.Activate
    Set oldActive = Nothing
End Sub

0

我测试过了,它可以正常工作。我从几个地方借鉴了一些部分。

Sub RR0()   'Replace Reset & Open dialog  (specs: clear settings, search columns, match case)

'Dim r As RANGE         'not seem to need
'Set r = ActiveCell     'not seem to need
On Error Resume Next    'just in case there is no active cell
On Error GoTo 0

Application.FindFormat.Clear          'yes
Application.ReplaceFormat.Clear       'yes

Cells.find what:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext
Cells.Replace what:="", Replacement:="", ReplaceFormat:=False, MatchCase:=True    'format not seem to do anything
'Cells.Replace what:="", Replacement:="", ReplaceFormat:=False    'orig, wo matchcase not work unless put here - in replace

'If Not r Is Nothing Then r.Select    'not seem to need
'Set r = Nothing

'settings choices:
'match entire cell:  LookAt:=xlWhole,  or:  LookAt:=xlPart,
'column or row:      SearchOrder:=xlByColumns,  or:  SearchOrder:=xlByRows,

Application.CommandBars("Edit").Controls("Replace...").Execute   'YES WORKS
'Application.CommandBars("Edit").Controls("Find...").Execute   'YES same, easier to manipulate
'Application.CommandBars.FindControl(ID:=1849).Execute        'YES full find dialog

'PROBLEM: how to expand options?
'SendKeys ("%{T}")   'alt-T works the first time, want options to stay open

Application.EnableEvents = True           'EVENTS

End Sub

0
没必要使用sendkeys,你可以轻松地引用需要重置对话框值的值。
Sub ResetFindReplace()
   'Resets the find/replace dialog box options
   Dim r As Range

   On Error Resume Next

   Set r = Cells.Find(What:="", _
   LookIn:=xlFormulas, _
   SearchOrder:=xlRows, _
   LookAt:=xlPart, _
   MatchCase:=False)

   On Error GoTo 0

   'Reset the defaults

   On Error Resume Next

   Set r = Cells.Find(What:="", _
   LookIn:=xlFormulas, _
   SearchOrder:=xlRows, _
   LookAt:=xlPart, _
   MatchCase:=False)

   On Error GoTo 0
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接