vba - 防止 Range.Find() 更改 "match entire cell content"用户界面设置

标签 vba excel

运行以下代码的第一行后,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/

相关文章:

Excel VBA 将 XL-2007 启用宏的工作簿复制为不带宏的 excel-2003 文件

vba - 数据类型 Number-Decimal

java - java代码的等效vb代码

excel - VBA停止在excel 2010中打印工作表

vba - 在 VBA/VBS 中退出 while 循环

vba - 更改私有(private)变量或定义范围变量。 Excel VBA

excel - 无法使用 Spring Boot 和 Apache POI 正确生成 Excel 文件

用于按升序对 Excel 列进行排序并扩展选择的 VBA 代码?

Excel VBA 运行时错误 '424' : Object Required when trying to copy TextBox

regex - 使用正则表达式和vba,提取部分数据