如何以编程方式将Excel Find and Replace
对话框参数重置为默认值("查找内容"、"替换为"、"内部"、"搜索"、"查找范围"、"大小写匹配"、"匹配整个单元格内容")?
我正在使用Application.FindFormat.Clear
和Application.ReplaceFormat.Clear
重置查找和替换单元格格式。
有趣的是,使用expression.Replace(FindWhat, ReplaceWhat, After, MatchCase, WholeWords)
后,FindWhat
字符串会显示在Find and Replace
对话框中,但不会显示ReplaceWhat
参数。
您可以使用此宏重置find&代替不幸的是,你必须同时调用它们,因为每个参数都有一到两个唯一的参数,所以如果你想重置所有内容,你就会陷入困境。没有"重置",所以我找到的唯一方法是执行一个假的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
您可以使用以下命令打开填充字段的"替换"对话框:
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上发布这种"更快"的方式,所以你可以试一试。
比替换快
无需使用sendkey,您可以轻松引用重置对话框值所需的值。
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
我对此进行了测试,它有效。我从的几个地方借了零件
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
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