gpt4 book ai didi

excel - 'Find' 命令的表达式

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

我想使用“查找”命令一次搜索一个单元格,而不是一次搜索一个单元格范围。我尝试使用如下所示的“For Each”命令,但它不起作用。是否可以使用其他表达式搜索单词,例如 Set rgFound = activecell.Find("John")Set rgFound = cells(1,1).Find("John"))除了使用“范围”?

Sub tester()
Dim rgFound As Range
Set rng = Columns(1)
Set rng1 = rng.SpecialCells(xlVisible)

For Each cell In rng1
Set rgFound = cell.Find("John")
If rgFound Is Nothing Then
MsgBox "Name was not found."
Else
MsgBox "Name found in :" & rgFound.Address
End If
Next
End Sub

最佳答案

要搜索单个单元格的值,您必须使用 InStr function

If InStr(1, Range("A1").Value, "John", vbTextCompare) > 0 Then
'John was found
Else
'John was not found
End If
InStr返回 John 的第一个位置在单元格 A1 的值中找到,所以如果它返回一个数字 > 0然后发现不然不行。

或者,您可以使用 Like operator :
If Range("A1").Value Like "*John*" Then
'John was found
Else
'John was not found
End If

注意周围的占位符( * 星号)以确保类似的运算符识别 John"any string that conains John in the begining middele or end of the string"

关于excel - 'Find' 命令的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61517021/

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