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

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

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

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

您可以使用此macros来重置查找和replace。 不幸的是,由于每个参数都有一个或两个独特的参数,所以您必须调用它们,所以如果您想重置所有的参数,那么您就被卡住了。 没有“重置”,所以我find的唯一方法是使用默认参数执行假发现和replace。

 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 

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

Application.Dialogs(xlDialogFormulaReplace).Show -arguments here-

参数列表是

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


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


经过大量的研究和testing,我现在确切地知道你想要做什么,但不要认为它可以做(没有SendKey)。 看起来,Excel中有一个错误,不会重置重置值(来自VBA),不pipe你尝试和设置。

我确实发现了在MSDN上发布的这种“更快”的方式,所以你可以试试看。

比replace更快

无需使用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 

我testing了这个,它的工作原理。 我从几个地方借了零件

 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