gpt4 book ai didi

vba - 为什么 Range.Find 方法不能找到字符串的第一个实例?

转载 作者:行者123 更新时间:2023-12-04 21:57:40 25 4
gpt4 key购买 nike

A1:I1 范围内,确切的字符串 "*ContactName"出现两次 - 在 A1 中出现一次并再次在 I1 . Here is a link to a picture of the range.

当我期望返回 1 时,下面的代码返回 9。

请问有人可以解释为什么吗?我已经阅读了微软的文档,但仍然无法弄清楚:https://msdn.microsoft.com/en-us/library/office/ff839746.aspx

Sub FindCol()
Dim destinationCol As Integer
destinationCol = Range("A1:I1").Find("*ContactName").Column
MsgBox destinationCol
End Sub

如果我将代码更改为从第 I 列开始,则如预期的那样,该方法将循环返回并返回 1(A 列)。
Sub FindCol()
Dim destinationCol As Integer
destinationCol = Range("A1:I1").Find("*ContactName", after:=Cells(1, 9)).Column
MsgBox destinationCol
End Sub

最佳答案

从您链接到的文档中,关于可选参数 After :

The cell after which you want the search to begin. This corresponds to the position of the active cell when a search is done from the user interface. Notice that After must be a single cell in the range. Remember that the search begins after this cell; the specified cell isn't searched until the method wraps back around to this cell. If you do no specify this argument, the search starts after the cell in the upper-left corner of the range.



您没有提供 After因此在这种情况下,它默认为 A1 .请注意文档中的部分内容,即“在方法回绕到此单元格之前,不会搜索指定的单元格”。

如果您使用:
destinationCol = Range("A1:I1").Find("*ContactName", Range("I1")).Column

它会按您的意愿工作。

关于vba - 为什么 Range.Find 方法不能找到字符串的第一个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41420760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com