运行以下代码的第一行后,Excel 会更改用户界面中的匹配整个单元格内容设置。结果是,下次用户按 Ctrl+F 时,搜索将在整个单元格内容中完成。我不喜欢更改影响用户体验的设置的 VBA 宏,因此我总是执行第二行,该行执行另一次搜索并将匹配整个单元格内容设置回默认值。
'execute the find and changes the entire cell content setting
Set C = MyRange.Find(SomeText, LookAt:=xlWhole)
'put the setting back to the default value (not the previous value)
MyRange.SomeText ParName, LookAt:=xlPart
问题在于它将其设置回默认值,而不是用户上次设置的值。
我宁愿有这样的东西:
'store the current entire cell content setting
OldLookAt = Application.FindLookAt
'execute the find
Set C = MyRange.Find(SomeText, LookAt:=xlWhole)
'restore the original setting
Application.FindLookAt = OldLookAt
我编写了 Application.FindLookAt
因为我无法找到它。
有没有办法在不影响用户体验的情况下Range.Find
某些内容?
最佳答案
我花了相当多的时间查看 Excel 对象库和 MSDN 文档,但似乎无法检索到。
不幸的是,如果您不将该参数包含为默认值,Excel 实际上也会覆盖并设置您的用户设置。
Private Sub testXLLookAT()
'Setup fake falues
Dim mCurLookup As XlLookAt
Range("A1").value = "Test1"
Range("A2").value = "Test"
'get current setting, but this doesn't work as it defaults...
If Range("A1:A2").Find("Test").Address = "A1" Then
mCurLookup = xlPart
Else
mCurLookup = xlWhole
End If
'your find here
'note - mCurLookup always becomes xlWhole...
Range("A1:A2").Find What:="Test", LookAt:=mCurLookup
End Sub
否则,上面的代码可以工作 - 但它不会,因为即使不包含默认值,它也会被覆盖。
关于vba - 防止 Range.Find() 更改 "match entire cell content"用户界面设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19283546/