如何以编程方式将 ExcelFind and Replace对话框参数重置为默认值(“查找内容”、“替换为”、“范围内”、“搜索”、“查找范围”、“匹配案例”、“匹配整个单元格内容”)?
我正在使用Application.FindFormat.Clear和Application.ReplaceFormat.Clear重置查找和替换单元格格式。
有趣的是,使用 后expression.Replace(FindWhat, ReplaceWhat, After, MatchCase, WholeWords),FindWhat字符串显示在Find and Replace对话框中,但不显示ReplaceWhat参数。
您可以使用此宏来重置查找和替换。不幸的是,您必须同时调用它们,因为每个参数都有一个或两个唯一的参数,因此如果您想重置所有内容,您就会陷入困境。没有“重置”,所以我发现的唯一方法是使用默认参数执行假查找和替换。
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
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令打开已填充字段的“替换”对话框:
Application.Dialogs(xlDialogFormulaReplace).Show -此处的参数-
参数列表是
find_text、replace_text、look_at、look_by、active_cell、match_case、match_byte
到目前为止,我发现“单击”按钮的唯一方法是使用 SendKey。
经过大量研究和测试,我现在确切地知道你想要做什么,但不认为它可以完成(没有 SendKey)。Excel 中似乎存在一个错误,无论您尝试将其设置为什么,都不会重置替换值(来自 VBA)。
我确实发现有人在 MSDN 上发布了这种“更快”的方法,所以你可以尝试一下。