我正在编写一个子例程,它查看从单元格 A1 开始的一系列单元格(范围为 1 列宽),其中包含字符串值。我的 sub 首先找到整个范围并将其分配给范围变量“theForest”,以帮助简化搜索。然后,它查看范围内的每个单元格,直到找到单词“Edward”。无论他是否被找到,它都会在一条消息中显示结果(说明他是否被找到)。
我到目前为止的代码是这样的:
With Range("A1")
'this will help find the entire range, since it is only one column I will search by going down
theForest = Range(.Offset(0,0), .End(xlDown)).Select
Dim cell As Range
For Each cell In theForest
If InStr(Edward) Then
Msgbox"He was found"
Else
Msgbox"He was not found sorry"
End If
Next cell
End With
但是我在运行程序时遇到了很多错误,我认为问题出在
theForest = Range(.Offset(0,0), .End(xlDown.)).Select
代码行。我很感激这个简单代码的任何指导。
谢谢 :)
编辑:这是我想出的一些新代码:
Dim isFound As Boolean
isFound = False
With Range("A1")
For i = 1 to 500
If .Offset(1,0).Value = "Edward" Then
isFound = True
Exit For
End If
Next
End With
If isFound = True Then
Msgbox " Edward was found"
Else
MsgBox "Edward was not found"
End if
再说一次,它不包括找到整个范围并将其分配给范围变量 theForest。
Dim theForest as Range, f as Range
Set theForest = ActiveSheet.Range(ActiveSheet.Range("A1"), _
ActiveSheet.Range("A1").End(xlDown))
Set f = theForest.Find("Edward", lookat:=xlWhole)
If Not f Is Nothing Then
Msgbox"He was found"
Else
Msgbox"He was not found sorry"
End If
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句