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

时间:2008-10-28 13:44:49

标签: excel vba

如何以编程方式将Excel Find and Replace对话框参数重置为默认值(“查找内容”,“替换为”,“内部”,“搜索”,“查找”,“匹配大小写”,“匹配整个单元格内容“)?

我正在使用Application.FindFormat.ClearApplication.ReplaceFormat.Clear重置查找和替换单元格格式。

有趣的是,使用expression.Replace(FindWhat, ReplaceWhat, After, MatchCase, WholeWords)后,FindWhat字符串会显示在Find and Replace对话框中,而不会显示在ReplaceWhat参数中。

4 个答案:

答案 0 :(得分:3)

您可以使用此宏重置find&更换。不幸的是,你必须同时调用它们,因为每个参数都有一个或两个独特的参数,所以如果你想重置所有内容,你就会被卡住。没有'重置',所以我找到的唯一方法是执行一个假的查找和放大器。使用默认参数替换。

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 :(得分:1)

您可以使用以下命令打开带有填充字段的“替换”对话框:

Application.Dialogs(xlDialogFormulaReplace).Show -arguments here

参数列表是

find_text,replace_text,look_at,look_by,active_cell,match_case,match_byte


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


经过大量研究和测试,我现在确切地知道你想做什么,但不认为可以做到(没有SendKey)。似乎Excel中存在一个错误,无论您尝试将其设置为是什么,都不会重置替换值(来自VBA)。

我确实发现有人在MSDN上发布这种“更快”的方式,所以你可以尝试一下。

Faster than Replace

答案 2 :(得分: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

答案 3 :(得分: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